Function.series >= 0.1.2

Purpose

Run (async) functions in serie

Syntax

Function.series ( Booleanforce_async= true Arraytasks Functioncallback );

Parameters

force_async
If set to false, the first task will be executed synchronously
tasks
Array of functions, or multiple functions
callback

Examples

Run an array of tasks

var tasks = [];

for (let i = 0; i < 5; i++) {
    tasks.push(function(next) {
        setTimeout(function() {
            next(null, i);
        }, 5)
    });
}

// 'done' will be called after all timeouts have fired,
// which is about 25ms
Function.series(tasks, function done(err, result) {
    
    console.log(err);
    >>> null
    
    console.log(result);
    >>> [0, 1, 2, 3, 4]
});

Use several function arguments as tasks

Function.series(function one(next) {
    next(null, 1);
}, function two(next) {
    next(null, 2);
}, function three(next) {
    next(null, 3);
}, function done(err, result) {
    console.log(err);
    >>> null
    
    console.log(result);
    >>> [1, 2, 3]
});