0.0.2 • Published 9 years ago

@yuanqing/barrier v0.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
9 years ago

barrier.js npm Version Build Status Coverage Status

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, cb is immediately called with the err. If multiple functions had errored, cb will only be called exactly once.
    • If no error had occurred, results is the result of all the functions.

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

License

MIT