chainjs
Chainjs call each async function step by step, let async function callback fairily.
Install
npm install chainjs
Usage
use in node:
var Chain = require('chainjs')
function beginStep (chain) {
console.log('initialize');
chain.next('none');
}
function step1 (chain, data) {
console.log(data);
chain.next('say hello');
}
Chain(beginStep)
.then(step1)
.final(function (chain) {
console.log(data);
});
API
Chain(beginHandler)
Instancing a chain and then likes call "then"
Chain(func );
.then(stepHandler /, 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)
.some()
When call chain.next in which handler of "some" step will be over current step.
Chain(func).then(func1).some(funcA1, funcA2, funcA3)
.start()
Start running the chain
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);
.end(finalParams)
End up chain steps, mark the chain as ending
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
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
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.