chainjs

Chainjs call each async function step by step, let async function callback fairily.

chainjs

An asynchronous callback's flow controller, chaining async function callbacks. Async methods calling flow make easy. I use it in node.js server and webapp.

npm install chainjs

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 in step 1')
    }, function (chain) {
        chain.wait(200, 'then go to next in step 2')
    }, function (chain) {
        chain.wait(100, 'then go to next in step 3')
    })
    .then(function (chaindata) {
        console.log(data); // --> then go to next in step 3 
        chain.next('say hello');
    })
    .final(function (chaindata) {
        console.log(data); // --> say hello 
    });

Each step's handler has been passed the chain instance as the first argument

Instancing a chain, if arguments is not empty, it will be call .then() with arguments automatically. If first argument is type of Array, that argument will be passed as arguments.

Chain(func /*, func1, ..., funcN*/);
// or  
Chain([func, func1, funcN])

Define a chain step, if a then step has multiple functions, it need each function call chain.next() to goto next step.

Chain().then(funcA1, funcA2, funcA3).then(func1)

If first argument is type of Array, that argument will be passed as arguments.

Chain.then([func1, func2, ..., funcN])
// equal to  
Chain.then(func1, func2, ..., funcN)

Call current function once again (use for recursive).

var flag
Chain(function (chaindata) {
    if (flag) {
        return chain.next()
    }
    flag = true
    chain.retry()
}).start('value')

Define a chain step, if a then step has multiple functions, it need any function of this step calling chain.next() only once to goto next step.

Chain(func).some(function (chain) {
    setTimeout(function () {
        chain.next()
    }, 100)
}, function (chain) {
    setTimeout(function () {
        chain.next()
    }, 1000)
}, function (chain) {
    setTimeout(function () {
        chain.next()
    }, 500)
}).then(function () {
    // this step will be run after 100ms. 
})

If first argument is type of Array, that argument will be passed as arguments.

Chain.some([func1, func2, ..., funcN])
// equal to  
Chain.some(func1, func2, ..., funcN)

Define a chain step, call each handlers of this step in sequence. In this step, each function call chain.next() to call next function. In orders from left to right of arguments

Chain(func).then(func1).each(funcA1, funcA2, funcA3)

If first argument is type of Array, that argument will be passed as arguments.

Chain.each([func1, func2, ..., funcN])
// equal to  
Chain.each(func1, func2, ..., funcN)

Start running the chain, and could pass data to initial step.

Chain(function (chaininitData) {
    
}).then(func1).then(func2).start(initData);

Destroy the chain, mark the chain as ending and destroy local variable, but don't calling final 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();

Go to next step

chain.next();
// pass params to next step handler 
chain.next(data);

Define a branch step, only using chain.nextTo(branchName) to goto branch step. Call chain.next() from last step will skip next branch step.

     -------------o
     |            ↓
o----o----->o---->o---->o
Chain(function (chain) {
    chain.nextTo('branchA')
    chain.next()
}).then(function (chain) {
    throw new Error('This step should not be called')
}).branch('branchA', function (chain) {
    chain.next()
}).branch('branchB', function (chain) {
    throw new Error('This step should not be called')
}).final(function (chain) {
    // done 
}).start()

Go to next branch.

Chain(function (chain) {
    chain.nextTo('branchA')
}).then(function (chain) {
    throw new Error('This step should not be called')
}).branch('branchA', function (chain) {
    chain.next()
})

Notice: .nextTo() should not goto previous step

Chain(function (chain) {
    chain.next()
}).branch('branchA', function (chain) {
    chain.next()
}).then(function (chain) {
    chain.nextTo('branchA') // will throw an error 
}).start()

Waiting some time then call next step.Just a shortcut of setTimeout(function () {chain.next()}, time).

// pass params to next step handler 
chain.wait(5000, data); // wait 5s then call next 

End up chain steps, mark the chain as ending, for cross steps data sharing

chain.end();
// pass params to final handler 
chain.end(data);

Define a final step, witch will be invoke after call chain.end() or all step of this chain is over.

Chain(function (chain) {
    ...
    chain.end('ending initial step')
 
}).then(function (chain) {
 
    ...
    chain.next('step 2 calling')
 
}).final(function (chaindata) {
    console.log(data) // --> ending initial step 
})

Saving data in current chain

// set data 
chain.data('param', param);
// set multiple data in batch 
chain.data({
    'param1': param1,
    'param2': param2,
    'param3': param3
});
// get data 
chain.data('param');
// get all data 
var chainData = chain.data();

Turn a regular node function into chainjs thunk.

 function handler1 (paramcallback) {
    callback(param + 'Chain through step1, ')
}
function handler2 (paramcallback) {
    callback(param + 'step2')
}
Chain()
    .then(Chain.thunk(handler1))
    .then(Chain.thunk(handler2))
    .final(function (chaindata) {
        console.log(data); // --> Initialize! Chain through step1, step2 
    })
    .start('Initialize! ')

Binding "this" to specified ctx for all functions of each step of current chain.

Chain(function () {
    console.log(this); // --> "abc" 
})
.then(function () {
    console.log(this); // --> "abc" 
})
.context('abc')
.start()

Chainjs using mocha for BDD test, run below cli to run testing in nodejs

npm test

See using chainjs control business logic flow example

var Chain = require('chainjs');
var someStepCount = 0;
var parallelCount = 0;
 
Chain(function (chain) {
        // save param 
        chain.data('chain:param', 'Chain initial step data');
        chain.next({message: 'Next step'});
    })
    .some(function (chainparam) {
        someStepCount ++;
        chain.wait(2000, 'step is "some-1"');
    }, function (chainparam) {
        someStepCount ++;
        chain.wait(1000, 'step is "some-2"');
    })
    .then(function (chainmsg) {
        console.log(msg); // step is "some-2 
        // Step over when last step has one hander called 
        console.log(someStepCount); // 1 
        chain.next();
    })
    .then(function (chain) {
        parallelCount ++;
        chain.next();
    }, function (chain) {
        parallelCount ++;
        chain.next();
    }, function (chain) {
        parallelCount ++;
        chain.next();
    })
    .then(function (chain) {
        // all handlers had been called in last step 
        console.log(parallelCount); // 3 
        chain.end();
    })
    .then(function (chain) {
        //prev step had call chain.end(), so this step will be skiped 
    })
    .final(function (chain) {
        var param = chain.data('chain:param');
        console.log(param); // "Chain initial step data" 
    })
    .context(this)
    .start(); // starting chain execute 

See change logs

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.