Function.parallel >= 0.1.2

Purpose

Run (async) functions in parallel

Syntax

Function.parallel ( Booleanforce_async= trueNumberlimitArraytasksFunctioncallback );

Parameters

force_async

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

limit

The amount of tasks that can run at the same time

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 5ms
Function.parallel(tasks, function done(err, result) {
    
    console.log(err);
    >>> null
    
    console.log(result);
    >>> [0, 1, 2, 3, 4]
});

Use several function arguments as tasks

Function.parallel(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