Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Asynchronous javascript coding made easy
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
examples
test
LICENSE
README.md
enq.js
package.json

README.md

Enqjs

Asynchronous calls made without messy nestings, in a simpler way to provide multiple callbacks.

The goal of this project is be usable in any ECMAScript environment (NodeJS, web browser javascript and others) and make longer calls more readable and simple.

Status

EnqJS is still in beta. If you have detect any bugs, please report!

How to install

» To use as node module, just install enq.js in your "node_modules" folder » To use in browsers, just put enq.js on your tree and call as a regular script.

Usage

EnqJS is an attempt of make the coding flow be the more clean possible. It makes use of chaining, i.e. You can make your code continuously group calls and its returns and the returns of its return...and so on.

You can run sequences of assynchronous functions using a kind of currying. Ex:

var x = enq(function(){
    var self=this;
    console.log('Running x)
    setTimeout(function(){ self.returns("x response") },300)

});

var y = enq(function(){
    var self=this;
    console.log('Running y)
    setTimeout(function(){ self.returns("y response") },300)

});

var z = enq(function(){
    var self=this;
    console.log('Running x)
    setTimeout(function(){ self.returns("z response") },300)

});

// z(...)

A group of functions can be set to run in parallel. They don't communicate each other. Ex:

enq(
    function(){
        console.log("I'm foo");
    },
    function(){
        console.log("I'm bar");
    }
);

This group alone isn't going to be useful. But if you need multiple functions running and a callback that be triggered only when both functions returns, this will help you much!

var x = enq(
    // This is the first group, containing 2 functions
    // The timeout is used as example of async behavior
    function(){
        var self=this;
        setTimeout(function(){ self.returns("I'm foo"); },900);
    },
    function(){
        var self=this;
        setTimeout(function(){ self.returns("I'm bar"); },800);
    }
)

var y = x(
    // This is the second group
    function(result1,result2){
        console.log('firstReturn = '+result1);
        console.log('secondReturn = '+result2);
    }
);

The functions of first group receive any parameters (because there isn't any groups before this to return results). But the second group of functions (and later) receive the returns of the immediately previous group of functions as parameters.

You can use multiple groups in the same chain. Each group will receive as parameter the return of previous group functions. To walk to next group, all the functions of a group need to call this.returns();

Ex: on func1 return, func2 is called. On func2 return, func3 is called... and so...

enq.js(func1)(func2)(func3)(funcX);

For groups with multiple functions, manage each return (and the function parameter list) can be messy. Another way to accomplish that is passing a object as the group of functions:

enq({
    func1:function(){this.returns("hey");},
    func2:function(){this.returns("you");},
    func3:function(){this.returns("I'm");},
    func4:function(){this.returns("here");}
})({
    funcX:function(res){
        this.returns(res.func1+' '+res.func2+' '+res.func3+' '+res.func4+' you see?!');
    },
    funcY:function(res){
        this.returns(res.func1+' '+res.func2+' '+res.func3+' '+res.func4+' again!');
    }
})(
    function(res){console.log(res.funcX+' AND '+res.funcY);}
);

In the example above, you can see that we can mix an object as group of functions in one call and a list of functions in other call.

That's all folks! Simple, with no needs of learn a lot of properties, methods and configs. The only thing you need is to plan the execution flow ;)

License

Copyright(c) 2011-2012 Thadeu de Paula

MIT license

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.

Something went wrong with that request. Please try again.