Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including spasm with all npm packages installed. Try it out:

var spasm = require("spasm")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

spasm v0.1.0

Spasm: super primitive asynchronous state machine

Spasm

Spasm is a super primitive asynchronous state machine for Node.js.

Build Status

Installation

$ npm install spasm

Usage

Spasm is extremely simple to use. Here’s an example using Commander.js to prompt the user for input from the command line until a non-empty string is provided:

var spasm   = require('spasm');
var program = require('commander');

var machine = spasm({
  'prompt': function(next, context) {
    program.prompt('What’s your name? ', function(name) {
      if ('' === name) {
        next('prompt');
      } else {
        context.name = name;

        next('display');
      }
    });
  },
  'display': function(next, context) {
    console.log(context.salutation, context.name);

    process.exit(0);
  }
});

machine('prompt', {
  salutation: 'Hello, %s.'
});

spasm() can be passed any object as a set of states, but it must have at least one state (a property with a function for a value). Each state will be passed a next function and the context of the state machine when it’s invoked. next uses process.nextTick() to ensure that each state is invoked asynchronously. The default context is an empty object ({}) if one isn’t passed when the machine is started. If a state makes any changes to context, it will automatically be reflected in any subsequent states. Alternatively, you can pass an entirely new context to next() as a second argument.

History

See http://github.com/jordanryanmoore/spasm/blob/master/History.md

License

See http://github.com/jordanryanmoore/spasm/blob/master/License

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free