Minimalist Flow Control with Step

Tim Caswell’s Step is a lightweight library.[42] In fact, its API consists of a single function: Step.

Step takes a list of functions; here’s an example:

 
Step(task1, task2, task3);

Each function can control the flow in three ways.

  • It can call this to make Step run the next function in the list.

  • It can call a callback generated by this.parallel or this.group n times to tell Step to run the next function n times.

  • It can return a value, which will also make Step run the next function in the list. This makes it easy to mix synchronous functions with async ones.

The next function will receive the results of its predecessor (that is, its return value or the arguments it passed to this) or the results of all instances of its predecessor if that predecessor was run with this.parallel or this.group. (The difference is that this.parallel provides those results as separate arguments, while this.group merges them into arrays.)

The entire library is, as of this writing, just 152 lines long (with comments) yet is versatile enough to handle most async flows. The downside of this minimalism is that a flow created with Step can be understood only by reading each function in it. Flows created with the everything-but-the-kitchen-sink Async.js tend to be more self-explanatory.

Still, if you feel like rolling up your sleeves, writing Async.js-like utility functions in Step can be a great exercise. For example, here’s the equivalent of async.map in just eleven lines:

Asyncjs/stepMap.js
 
var​ Step = require(​'step'​);
 
 
function​ stepMap(arr, iterator, callback) {
 
Step(
 
function​() {
 
var​ group = this.group();
 
for​ (​var​ i = 0; i < arr.length; i++) {
 
iterator(arr[i], group());
 
}
 
},
 
callback
 
);
 
}

I find using Step to be a refreshing exercise. While using Async.js is mainly about finding the right utility function for the job, Step encourages you to think problems through clearly and write elegant, efficient solutions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.26.204