parallel

Parallel execution patterns

Source:

Methods

(static) each(items, task, optionsopt, callbackopt)

Run items on async task function in parallel.

Does not stop parallel execution on errors. All tasks get executed.

Parameters:
Name Type Attributes Description
items Array

Array of items any[]

task function

iterator function of type function (item: any, cb: Function, index: Number)

options Object <optional>
Properties
Name Type Attributes Description
timeout Number <optional>

timeout in ms which throwing AsynccError in case that tasks are still running

bail Boolean <optional>

bail-out on first error

callback function <optional>

optional callback function called by last terminating function from tasks, needs to be of type function (err: AsynccError, result: Array<any>) where err.errors is an Array containing the errors in the same order as the res results array. err.errpos gives the positions of errors in order as they occur.

Source:
Example
each([1, 2, 3],
  (item, cb, index) => {
    cb(index % 2 ? null : 'error', item + index)
  }, (err, res) => {
    //> err.errors = [null, 'error', null]
    //> err.errpos = [1]
    //> res = [1, 4, 5]
  }
)

(static) eachLimit(limit, items, task, optionsopt, callbackopt)

Run items on async task function in parallel limited to limit parallel.

Does not stop parallel execution on errors. All tasks get executed.

Parameters:
Name Type Attributes Description
limit Number

number of tasks running in parallel

items Array

Array of items any[]

task function

iterator function of type function (item: any, cb: Function, index: Number)

options Object <optional>
Properties
Name Type Attributes Description
timeout Number <optional>

timeout in ms which throwing AsynccError in case that tasks are still running

bail Boolean <optional>

bail-out on first error

callback function <optional>

optional callback function called by last terminating function from tasks, needs to be of type function (err: AsynccError, result: Array<any>) where err.errors is an Array containing the errors in the same order as the res results array. err.errpos gives the positions of errors in order as they occur.

Source:
Example
eachLimit(2, [1, 2, 3, 4],
  (item, cb, index) => {
    cb(index % 2 ? null : 'error', item + index)
  }, (err, res) => {
    //> err.errors = [null, 'error', null, 'error']
    //> err.errpos = [1, 3]
    //> res = [1, 4, 5, 7]
  }
)

(static) parallel(tasks, optionsopt, callbackopt)

Run tasks callback functions in parallel.

Does not stop parallel execution on errors. All tasks get executed. The optional callback gets called after the longest running task finishes.

Parameters:
Name Type Attributes Description
tasks Array.<function()>

Array of callback functions of type function (cb: Function)

options Object <optional>
Properties
Name Type Attributes Description
timeout Number <optional>

timeout in ms which throwing AsynccError in case that tasks are still running

bail Boolean <optional>

bail-out on first error

callback function <optional>

optional callback function called by last terminating function from tasks, needs to be of type function (err: AsynccError, result: Array<any>) where err.errors is an Array containing the errors in the same order as the res results array. err.errpos gives the positions of errors in order as they occur.

Source:
Example
parallel([
  (cb) => { cb(null, 1) },
  (cb) => { cb('error', 2) },
  (cb) => { cb(null, 3) }
], (err, res) => {
  //> err.errors = [null, 'error', null]
  //> err.errpos = [1]
  //> res = [1, 2, 3]
})

(static) parallelLimit(limit, tasks, optionsopt, callbackopt)

Run tasks callback functions in parallel limited to limit parallel running tasks.

Does not stop parallel execution on errors. All tasks get executed. The optional callback gets called after the longest running task finishes.

Parameters:
Name Type Attributes Description
limit Number

number of tasks running in parallel

tasks Array

Array of callback functions of type function (cb: Function)

options Object <optional>
Properties
Name Type Attributes Description
timeout Number <optional>

timeout in ms which throwing AsynccError in case that tasks are still running

bail Boolean <optional>

bail-out on first error

callback function <optional>

optional callback function called by last terminating function from tasks, needs to be of type function (err: AsynccError, result: Array<any>) where err.errors is an Array containing the errors in the same order as the res results array. err.errpos gives the positions of errors in order as they occur.

Source:
Example
// runs 2 tasks in parallel
parallelLimit(2, [
  (cb) => { cb(null, 1) },
  (cb) => { cb('error', 2) },
  (cb) => { cb(null, 3) }
], (err, res) => {
  //> err.errors = [null, 'error', null]
  //> err.errorpos = [1]
  //> res = [1, 2, 3]
})

(static) queue(task, concurrency) → {Queue}

Run queued items through an asynchronous task.

Once finishing the task an optional callback is called. While pushing to the queue, you may define a priority for execution. Lower values means faster execution.

See full API here Queue.

Parameters:
Name Type Description
task function

iterator function of type function (item: any, cb: Function, index: Number)

concurrency Number

max. number of tasks running in parallel

Source:
Returns:
Type
Queue