0.0.2 • Published 10 years ago
@yuanqing/barrier v0.0.2
barrier.js

An implementation of the Barrier pattern in JavaScript.
Usage
'use strict';
var barrier = require('@yuanqing/barrier');
var b = barrier(2, function(err, result) {
if (err) {
throw err;
}
console.log(result); //=> ['foo', 'bar']
});
setTimeout(function() {
b(0, null, 'foo');
}, 0);
setTimeout(function() {
b(1, null, 'bar');
}, 100);API
var barrier = require('@yuanqing/barrier');var b = barrier(num, cb)
num— The number of functions we want to wait for at the barrier.cb— A function that is called when all the functions have reached the barrier. Its signature is(err, results).- If any one of the functions we were waiting for had errored,
cbis immediately called with theerr. If multiple functions had errored,cbwill only be called exactly once. - If no error had occurred,
resultsis the result of all the functions.
- If any one of the functions we were waiting for had errored,
b(index , err, result)
b is a function that is invoked to signal that a particular function has reached the barrier.
index— An index associated with the function.err— Set this to a truthy value to indicate that an error had occurred.result— The result of the function; optional.
b must be invoked with index set to each number in the range [0, num). So, in our example, given that num is 2, we invoke b once with index set to 0, and another time with index set to 1.
Installation
Install via npm:
$ npm i --save @yuanqing/barrier