chainjs
Chaining of functions call to resolve the problem of javascript async callback handling.
Install
npm install chainjs
Usage
use in node:
var Chain = require('chainjs'),
param = {message: 'Hello world';}
function beginStep (chain, param) {
console.log(param.message);
chain.next('none');
}
function step1 (chain, data) {
chain.next('say hello');
}
Chain(beginStep, param)
.then(step1)
.final(function (chain, data) {
console.log(data);
});
API
Chain(beginHandler)
Chain(func, param)
then(stepHandler)
Chain(func).then(func1).then(func2)
start()
Chain(func).then(func1).then(func2).start();
next(nextParams)
end(finalParams)
final(finalHandler)
data(savingData)
Saving data in current chain
Chain(function (chain, param) {
chain.data('param', param);
chain.next();
}, {data: ''})
.then(function (chain) {
var param = chain.data('param');
console.log(param);
})
.start();
filter(filterHandler)
TBD
before(beforeHandler)
Will be invoked before each step
Chain(function (chain, param) {
chain.data('param', param);
chain.next();
}, {data: ''})
.then(function (chain) {
var param = chain.data('param');
console.log(param);
})
.before(function (chain) {
console.log('It will be invoked before each chain step')
})
.start();
after(afterHandler)
TBD
Testing
npm test
Example
var Chain = require('chainjs');
Chain(function (chain, msg) {
chain.data('chain:param', msg);
console.log(msg);
chain.next({message: 'Next step'});
}, 'Hello world')
.then(function (chain, param) {
console.log(param.message);
chain.end({name: 'switer'})
})
.then(function (chain) {
})
.final(function (chain, author) {
var param = chain.data('chain:param');
console.log(param);
console.log(author.name);
})
.start();
Change Log
License
MIT