chainjs
An asynchronous callback's flow controller, chaining async function callbacks. simple and clear than using promise. I use it in node.js server and webapp.
Install
npm install chainjs
Usage
use in node:
var Chain = require('chainjs')
Chain(function (chain) {
console.log('initialize');
chain.next('none');
})
.some(function (chain) {
chain.wait(300, 'then go to next')
}, function (chain) {
chain.wait(200, 'then go to next')
}, function (chain) {
chain.wait(100, 'then go to next')
})
.then(function (chain, data) {
console.log(data);
chain.next('say hello');
})
.final(function (chain, data) {
console.log(data);
});
API
Each step's handler has been passed the chain
instance as the first argument
Chain(func, func1, ..., funcN)
Instancing a chain and define a chain step
Chain(func );
.then(func, func1, ..., funcN)
Define chain steps, if a then step has multiple functions, step over after each func call chain.next()
Chain(func).then(func1).then(funcA1, funcA2, funcA3)
.retry()
Call current step handler once again (recursive).
var flag
Chain(function (chain, data) {
if (flag) {
return chain.next()
}
flag = true
chain.retry()
}).start('value')
.some(func, func1, ..., funcN)
When call chain.next in which handler of "some" step will be over current step.
Chain(func).then(func1).some(funcA1, funcA2, funcA3)
.each(func, func1, ..., funcN)
Call each handlers in sequence.
Chain(func).then(func1).each(funcA1, funcA2, funcA3)
.start(data)
Start running the chain, and could pass data to initial step.
Chain(func).then(func1).then(func2).start();
.destroy()
Destroy the chain, mark the chain as ending and destroy local variable, but not call ending funtions
notice: after use chain.destroy(), the chain contiue execute current step handler,
so use with return for stoping current step excution
Chain(func).then(function (chain) {
chain.destroy();
return;
}).start();
.next(nextParams)
Go to next step
chain.next();
chain.next(data);
.wait(time, nextParams)
Waiting some time then call next step
chain.wait(5000, data);
.end(finalParams)
End up chain steps, mark the chain as ending, for cross steps data sharing
chain.end();
chain.end(data);
.final(finalHandler)
Pushing final handler which will be invoke when chain.end() or chain step is ending
.data(savingData)
Saving data in current chain
chain.data('param', param);
chain.data('param');
var chainData = chain.data();
Testing
Chainjs using mocha for BDD test, run below cli to run testing in nodejs
npm test
Example
var Chain = require('chainjs');
var someStepCount = 0;
var parallelCount = 0;
Chain(function (chain) {
chain.data('chain:param', 'Chain initial step data');
chain.next({message: 'Next step'});
})
.some(function (chain, param) {
someStepCount ++;
chain.wait(2000, 'step is "some-1"');
}, function (chain, param) {
someStepCount ++;
chain.wait(1000, 'step is "some-2"');
})
.then(function (chain, msg) {
console.log(msg);
console.log(someStepCount);
chain.next();
})
.then(function (chain) {
parallelCount ++;
chain.next();
}, function (chain) {
parallelCount ++;
chain.next();
}, function (chain) {
parallelCount ++;
chain.next();
})
.then(function (chain) {
console.log(parallelCount);
chain.end();
})
.then(function (chain) {
})
.final(function (chain) {
var param = chain.data('chain:param');
console.log(param);
})
.context(this)
.start();
Change Log
See change logs
License
The MIT License (MIT)
Copyright (c) 2013 guankaishe
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.