Function.series >= 0.1.2

Purpose

Run (async) functions in serie

Syntax

Function.series ( Booleanforce_async= trueArraytasksFunctioncallback );

Parameters

force_async

If set to false, the first task will be executed synchronously

tasks

Array of functions, or multiple functions

callback

Return values

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]
});

Comments

Joy

Hi there! Such a nice post, thank you!

Katrina

Your internet site has exceptional web content. I bookmarked the site

Kristofer

Do you have any kind of ideas for creating short articles? That's where I always struggle and I simply wind up staring vacant screen for long time.