Sheaf
An API to make looping over a series of async activities one at a time more
straightforward.
Essentially it helps you code in a more synchronous manner than is convenient
with many NodeJS libraries. Since much of Node's value is exactly it's async
nature you should use with caution!
It is not the same promise.seq() in the node-promise, in that it
is designed to work on a series of values through a chain of functions, rather
than a single series of promise-returning functions.
You supply an array of initial values as the first argument to sheaf.
Subsequent arguments should be functions that return either a value or a promise
which resolves with a value. Starting with the values from the list, the output
from each of function is given as the argument to the next. As each run through
is complete, the next item in the list is used in the same way.
Sheaf itself returns a promise which resolves with an array of final values.
Here's a diagram of how it works:
Installation:
$ npm install sheaf
Example:
var urls = ['a.html', 'b.html'];
var getu = function(u) {
return $.get(u);
};
var parse = function(data) {
dfd = new $.Deferred();
jsdom.env(data, [], function(win) {
dfd.resolve(win);
});
return dfd.promise();
};
var check = function(win) {
var val = false;
return win.$('p').each(function() {
if ( win.$(this).html().match(/^\s+/) ) {
val = true;
}
}
return val;
};
sheaf(urls, getu, parse, check)
.then(
function(allChecks) {
console.log('all finished', allChecks);
},
null,
function() {
console.log('there was some progress');
}
);