fanin

fanin - the fan-in concurrency pattern

npm install fanin
6 downloads in the last week
24 downloads in the last month

node-fanin Build Status

A simple concurrency pattern: fan-in

Status: Untested in production, but ready to be.

Why do you need fan-in?

  • You want to make n asynchronous calls simultaneously and make a callback when all have finished.
  • You may want to save the callback values of these calls, and definitely want the errors.
  • You just want that. Nothing else.

API

Usage:

var fanin = require('fanin')
  , fan = fanin(number_of_callbacks, final_callback);

asyncCall(fan); // repeat...
asyncCall(fan.capture('field'));
asyncCall(fan.ordered(index));
...
fan.timeout(err)


// final_callback( error_array, return_value_object );

Basic Usage (no return values, just an unorded list of errors):

var fan = fanin(2, cb);

foo(fan);
bar(fan);

function foo(cb) { cb(new Error('some error')); }

function bar(cb) { cb(null); }

// cb([ Error('some error') ]);

Storing return value(s):

var fan = fanin(2, cb);

foo(fan.capture('foo'));
bar(fan.capture('bar'));

function foo(cb) { cb(null, 'foobaz'); }

function bar(cb) { cb(null, 'bar', 'baz'); }

// cb(undefined, { foo: 'foobaz', bar: [ 'bar', 'baz' ] } );

Storing return values in an ordered array:

var fan = fanin(2, cb);

foo(fan.ordered(1));
bar(fan.ordered(0));

function foo(cb) { cb(null, 'foobaz'); }

function bar(cb) { cb(null, 'barbaz'); }

// cb(undefined, { ordered: [ 'barbaz', 'foobaz' ] } );

Timeouts on these calls:

var fan = fanin(2, cb);

fan.timeout(new Error('timeout!'));

// cb([ Error('timeout!') ])

Installation

npm install fanin

Tests

npm test

License

MIT License found in the LICENSE file.

npm loves you