A simple control-flow library for node.JS that makes parallel execution, serial execution, and error handling painless.

After making the highly popular “Do” library for making node.JS easier to wrangle, Tim Caswell (aka creationix), gave in and learned to do async code the proper node way.

“Do” was an effort to convert basic node callback style functions into “Continuable” style functions. This is great, but now with the Step library you can use callback based functions directly without the added conversion step to another paradigm.

How To Use

The step library exports a single function called Step. It accepts any number of functions as arguments and runs them in serial order using the passed in this context as the callback to the next step.

var Step = require('step');

Step(
  function readSelf() {
    fs.readFile(__filename, this);
  },
  function capitalize(err, text) {
    if (err) {
      throw err;
    }
    return text.toUpperCase();
  },
  function showIt(err, newText) {
    sys.puts(newText);
  }
);

If you’ve seen Will Conant’s flow-js library then the pattern should look familiar. It’s based on the same idea, but optimized for the node.js environment. The first argument to all steps is always the error of the previous state if there was one (including any exceptions thrown). Also, you can either return the new value of call on of the “this” or “this.parallel()” callbacks when the next step is ready.

source on github


Have comments? Send a tweet to @TheChangelog on Twitter.

Subscribe to The Changelog Weekly – our weekly email covering everything that hits our open source radar.