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.0-1
  • Source
  • npm
  • Socket score

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

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); // --> none
    chain.next('say hello');
}

Chain(beginStep)
    .then(step1)
    .final(function (chain) {
        console.log(data); // --> say hello
    });

API

Chain(beginHandler)

Instancing a chain and then likes call "then"

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

.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();
// pass params to next step handler
chain.next(data);

.end(finalParams)

End up chain steps, mark the chain as ending

chain.end();
// pass params to final handler
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

// set data
chain.data('param', param);
// get data
chain.data('param');
// get all data
var chainData = chain.data();

Testing

npm test

Example

var Chain = require('chainjs');

Chain(function (chain, msg) {
        // save param
        chain.data('chain:param', msg);

        console.log(msg); //Hello world
        chain.next({message: 'Next step'});

    }, 'Hello world')
    .then(function (chain, param) {

        console.log(param.message); // Next step
        chain.end({name: 'switer'})
    })
    .then(function (chain) {
        //will be skiped
    })
    .final(function (chain, author) {
        var param = chain.data('chain:param');

        console.log(param); // Hello world
        console.log(author.name); // switer

    })
    .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 17 Nov 2014

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