root-chain

1.0.1 • Public • Published

Chaining

Clean up callback hell. All calls are currently sequential.

function sendRequest(url, callback) {
    $.get(url, function(response) {
        callback(response);
    });
}
 
chain(sendRequest, "http://www.rootof.com/")
.then(function(response, next) {
    // `.then` passes the previous callbacks params
    console.log(response);
})
.and(function(next) {
    // `.and` does not pass any parameters from previous call
    next(1);
})
.thenSync(function(param) {
    // `.thenSync` does not need `next()` call
    console.log(param + 1);
})
.andSync(function() {
    // `.andSync` does not need `next()` call
})
.and(function(next) {
    // return `chain.exit` to stop a chain
    return chain.exit;
})
.and(function(next) {
    // Will not get called
});
 
chain(sendRequest, "http://www.api.com/")
.then(sendRequest, "http://www.foo.com/")
.then(sendRequest, "http://www.bar.com/")
.end(function() {
    console.log("They are done");
})

Package Sidebar

Install

npm i root-chain

Weekly Downloads

0

Version

1.0.1

License

MIT

Last publish

Collaborators

  • erikrothoff