Asyncronous Iterators

sb.each(array, callback_loop(item, index, next_method, end_method), callback_end);

Runs next function when "next" method is executed.

var list = ['a', 'b', 'c'];
sb.each(list, function (item, i, next, end) {
    console.log("item: " + item );
    setTimeout(function () {
        next();
    }, 3000);
}, function () {
    console.log("End");
});
-> item: a // Wait 3 seconds;
-> item: b // Wait 3 seconds;
-> item: c // Wait 3 seconds;
-> End

Call "end" method for break the loop.

var list = ['a', 'b', 'c'];
sb.each(list, function (item, i, next, end) {
    console.log("item: " + item );
    setTimeout(function () {
        if (i === 1) end("Bye!");
        else next();
    }, 3000);
}, function (data) {
    if (data) console.log("End: " + data);
    else console.log("End");
});
-> item: a // Wait 3 seconds;
-> item: b // Wait 3 seconds;
-> End: Bye!

sb.eachParallelLimit(array, number_limit, callback_loop(item, index, next_method), callback_end);

Runs in parallel limit and next loop when "next" method is executed. Alternative names: eachpl, epl.

var list = ['a', 'b', 'c', 'd'];
sb.epl(list, 2, function (item, i, next) {
    console.log("item: " + item );
    setTimeout(function () {
        next();
    }, 2000);
}, function () {
    console.log("End");
});
-> item: a
-> item: b
-> item: c // Next to start when next method executed;
-> item: d // Next to start when next method executed;
-> End

sb.waterfall(array_functions(done, data), callback_end);

Runs next function when "done" method is executed. Alternative name: wf.

sb.waterfall([
    function (done) {
        console.log("fire");
        done(5);
    },
    function (done, data) {
        console.log("ice: " + data);
        done("win");
    }
], function (data) {
    console.log("End: " + data);
});
-> fire
-> ice: 5
-> End: win

Break the waterfall with "true".

sb.wf([
    function (done) {
        console.log("fire");
        done(true, 5);
    },
    function (done, data) {
        console.log("ice: " + data);
        done("win");
    }
], function (data) {
    console.log("End: " + data);
});
-> fire
-> End: 5

sb.parallel(array_functions(done), callback_end(data));

Run functions in parallel and then execute "callback_end".

sb.parallel([
    function (done) {
        setTimeout(function () {
            console.log("hi 3!");
            done("a")
        }, 3000);
    },
    function (done) {
        setTimeout(function () {
            console.log("hi 2!");
            done("b");
        }, 2000);
    },
    function (done) {
        setTimeout(function () {
            console.log("hi 1!");
            done("c");
        }, 1000);
    }
],function (data) {
        console.log("End: " + JSON.stringify(data));
});
//Wait 1 second
-> hi 1! //Wait 1 second
-> hi 2! //Wait 1 second
-> hi 3!
-> End: ["a","b","c"]

sb.parallelLimit(number, array_functions(done), callback_end(data));

Run limit of functions in parallel and then execute "callback_end". Alternative name : pl

sb.pl(2, [
    function (done) {
        setTimeout(function () {
          console.log("go 1!");
          done("a")
        }, 1000);
    },
    function (done) {
        setTimeout(function () {
        console.log("go 2!");
        done("b")
    }, 3000); },
    function (done) {
        setTimeout(function () {
            console.log("go 3!");
            done("c")
        }, 1000);
    },
    function (done) {
        setTimeout(function () {
            console.log("go 4!");
            done("a2")
        }, 3000);
    },
    function (done) {
        setTimeout(function () {
            console.log("go 5!");
            done("b2")
        }, 1000);
    },
    function (done) {
        setTimeout(function () {
            console.log("go 6!");
            done("c2")
        }, 3000); },
    function (done) {
        setTimeout(function () {
            console.log("go 7!");
            done("a3")
        }, 1000); },
    function (done) {
        setTimeout(function () {
            console.log("go 8!");
            done("b3")
        }, 3000);
    },
    function (done) {
        setTimeout(function () {
            console.log("go 9!");
            done("c3")
        }, 1000);
    }
],function (data) {
    console.log("we: " + JSON.stringify(data));
});
-> go 1!
-> go 3!
-> go 2!
-> go 5!
-> go 4!
-> go 7!
-> go 6!
-> go 9!
-> go 8!
-> we: ["a","b","c","a2","b2","c2","a3","b3","c3"]

sb.forever(callback(repeat, end), callback_end);

Loops syncronous forever. Alternative name: fe.

sb.forever(function (repeat, end) {
    console.log("Hi!")
    setTimeout(function () {
        repeat();
    }, 3000);
});
-> Hi! // Wait 3 seconds;
-> Hi! // Wait 3 seconds;
-> ...

Breaking forever loop.

var i = 0;
sb.fe(function (repeat, end) {
    console.log("loop: " + i);
    if (i>=3) end("Now Break!!");
    i++;
    setTimeout(function () {
        repeat();
    }, 3000);
}, function (data) {
    console.log("Response: " + data);
});
-> loop: 0 // Wait 3 seconds;
-> loop: 1 // Wait 3 seconds;
-> loop: 2 // Wait 3 seconds;
-> loop: 3 // Wait 3 seconds;
-> Response: Now Break!!

sb.times(number, callback(index, next, end), end);

Iterates function "number" times.

sb.times(5, function (index, next, end) {
        console.log("iterator: " + index);
        if (index === 3) end()
        else next();
}, () => {
        console.log("End!")
})
-> Iterator 0
-> Iterator 1
-> Iterator 2
-> Iterator 3
-> End!
sb.times(5, function (index, next, end) {
        console.log("Iterator " + index);
        next();
})
-> Iterator 0
-> Iterator 1
-> Iterator 2
-> Iterator 3
-> Iterator 4

sb.for(initial, final, increment, callback(index, next, end), callback_end(data));

Syncronous "for" iterator. Alternative name: forSync.

sb.for(0, 10, 1, function (index, next, end) {
    console.log(index)
    next(5)
}, function (data) {
    console.log(data)
})
-> 0
-> 1
-> 2
-> 3
-> 4
-> 5
-> 6
-> 7
-> 8
-> 9
-> [ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 ]

results matching ""

    No results matching ""