Chainlink 
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
- 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
- callback
Function
: The function to unlink from the chain.
Returns
An array of all callbacks unlinked from the chain.
.invoke(done, args...)
Arguments
- 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.
- 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.
Callback link/unlink
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.