man

Provides a series of helper functions that allow you to perform asynchronous iteration and flow control.

npm install man
1 downloads in the last day
11 downloads in the last week
27 downloads in the last month

man

Node.js package. Provides a series of helper functions that allow you to perform asynchronous iteration and flow control.

Installation

npm install man

Callbacks aggregation

If you want to receive results of several callbacks at the same time, use callback() method:

var fs = require('fs'),
    man = require('man'),
    cb = man.callback();

fs.readFile('file1.txt', cb.callback);
fs.readFile('file2.txt', cb.callback);

cb.on(function (err, content) {
    // On each callback.
});

cb.done(function (result1, result2) {
    // When all callbacks have received the results.
});

Serial execution

var man = require('man');

man.do(function () {
    fs.readFile('file1.txt', this.next);
}).do(function (err, content) {
    fs.readFile('file2.txt', this.next);
}).do(function (err, content) {
    // ...
});

Parallel execution

var man = require('man');

man.doAsync(function () {
    // ...
    this.done(result1);
}).do(function () {
    // ...
    this.done(result2);
}).then(function (result1, result2) {
    // ...
});

Iterations

Serial iterations

var man = require('man');

man.for([1, 2, 3, 4, 5]).do(function (number) {
    // On each item.
    this.done(number + 1);
}).then(function (results) {
    // When all items have processed.
    console.log(results); // [2, 3, 4, 5, 6]
});

Parallel iterations

var man = require('man');

man.forAsync([1, 2, 3, 4, 5]).do(function (number) {
    // On each item.
    this.done(number + 1);
}).then(function (results) {
    // When all items have processed.
    console.log(results); // [2, 3, 4, 5, 6]
});
npm loves you