Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
JavaScript async flow control
CoffeeScript JavaScript
Branch: master
Failed to load latest commit information.
lib compile with CoffeeScript 1.6.2
src add comment
test compile with CoffeeScript 1.6.2
.gitignore Initial commit
.npmignore Initial commit
LICENSE Initial commit
Makefile Initial commit
README.md English error
README_ja.md add README_ja.md
package.json bump version to 0.1.2

README.md

sequent

JavaScript async flow control

How to use

Wait (Join)

Execute after expected number of seq.done() are executed.

var Sequent = require('sequent');
var seq = new Sequent;

setTimeout(function(){
    seq.done("done 1"); // Arguments are optional
}, 500);

setTimeout(function(){
    seq.done("done 2", "end");
}, 1000);

// Wait for two seq.done()
seq.wait(2, function(arg1, arg2){
    // arg1 == "done 2", arg2 == "end"
    console.log("all done");
});

Break (as in for loops)

Exit waiting earlier than planned.

var Sequent = require('sequent');
var seq = new Sequent;

seq.wait(100, function(arg){
    // arg == "matured"
});

for (var i=0; i<100; i++) {
    if (i == 5) {
        // Break here and go to callback of wait()
        seq.mature("matured");
    }

    // seq.done() after seq.mature() are ignored
    seq.done("done " + i);
}

Queue (execute in order)

Execute functions in queued order when it becomes available.

var Sequent = require('sequent');
var seq = new Sequent;

var funcs = [];
var results = [];
for (var i=1; i<=10; i++) {
    // Functions will be executed in seq.queue()'d order.
    // seq.queue() returns a wrapped function.
    funcs.push(seq.queue(
        (function(_i){
            return function(){
                results.push(_i);
            }
        })(i)
    ));
}

funcs.reverse();

// Mark queued functions as available
for (var i=0, l=funcs.length; i<l; i++) {
    funcs[i]();
}

// Wait for all queued functions to be executed
seq.flush(function(){
    // results is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
});

Installation

npm install sequent
Something went wrong with that request. Please try again.