Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

chainjs

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chainjs

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

  • 0.1.7
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-66.67%
Maintainers
1
Weekly downloads
 
Created
Source

chainjs

logo

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.

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

diagram

API

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

Chain(func, func1, ..., funcN)

Instancing a chain, if arguments is not empty, it will be call .then() with arguments automatically.

Chain(func /*, func1, ..., funcN*/);

.then(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)

.retry()

Call current function once again (use for recursive).

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

.some(func, func1, ..., funcN)

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.
})

.each(func, func1, ..., 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)

.start(data)

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

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

.destroy()

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();

.next(nextParams)

Go to next step

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

.wait(time, nextParams)

Waiting some time then call next step

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

.end(finalParams)

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

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

.final(finalHandler)

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 (chain, data) {
    console.log(data) // --> ending initial step
})

.data(savingData)

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();

.thunk(func)

Turn a regular node function into chainjs thunk.

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

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) {
        // save param
        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); // 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

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.

Keywords

FAQs

Package last updated on 05 Jan 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc