JSPM

  • Created
  • Published
  • Downloads 775
  • Score
    100M100P100Q78239F
  • License MIT

An asynchronous task container.

Package Exports

  • chainlink

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (chainlink) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Chainlink Build Status

An asynchronous task container.

Installation

npm install chain --save

Example

var Chain = require('chainlink');
var chain = new Chain();

chain.link(function(next, arg) {
    console.log(arg);
    next('bar');
});

chain.link(function(next, arg) {
    console.log(arg);
    next('baz');
});

chain.invoke(function(err, arg) {
    console.log(arg);
}, 'foo');

Output:

foo
bar
baz

API

new Chain()

Takes no arguments. Returns a new Chain instance.

.link(callback)

Arguments

  1. callback Function(next, args...): The function to link to the chain.
    • Callbacks will be passed a next() method which must be called, and all of the arguments passed to .invoke() or the last call to next().
    • Any arguments passed to the next() method will be passed to the next callback in the chain.
    • Callbacks can throw an exception synchronously or call the next.error(err) method to skip all remaining callbacks in the chain. The done callback passed to invoke will be called and passed the error as a non-null first argument.

Returns

A reference to the chain.

.unlink(callback)

Arguments

  1. callback Function: The function to unlink from the chain.

Returns

An array of all callbacks unlinked from the chain.

.invoke(done, args...)

Arguments

  1. done Function(err, args...): The function to be called after all callbacks in the chain have been called.
    • The err parameter will be null if no callbacks threw synchronous errors or called the next.error(err) method, otherwise it will be the raised error.
  2. args...: All the arguments passed to the next() method by the last callback in the chain.

Returns

A reference to the chain.

Behavior

Recursion

Callbacks can recursively invoke the chain without any adverse effects.

If a callback calls the link or unlink method of its own chain, the current invocation will not be affected. Future invocations of the chain will reflect the linked or unlinked callbacks.