parallel

Parallel execution patterns

Source:

Methods

(static) each(items, task, 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)

callback function <optional>

optional callback function called by last terminating function from tasks, needs to be of type function (errors: Array<Error>, result: Array<any>, errpos: Array<Number>) where err is either null or an Array containing the errors in the same order as the res results array. 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, errpos) => {
    //> err = [ , 'error', ]
    //> res = [1, 4, 5]
    //> errpos = [1]
  }
)

(static) eachLimit(limit, items, task, 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)

callback function <optional>

optional callback function called by last terminating function from tasks, needs to be of type function (errors: Array<Error>, result: Array<any>, errpos: Array<Number>) where err is either null or an Array containing the errors in the same order as the res results array. 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, errpos) => {
    //> err = [ , 'error', , 'error']
    //> res = [1, 4, 5, 7]
    //> errpos = [1, 3]
  }
)

(static) parallel(tasks, 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)

callback function <optional>

optional callback function called by last terminating function from tasks, needs to be of type function (errors: Array<Error>, result: Array<any>, errpos: Array<Number>) where err is either null or an Array containing the errors in the same order as the res results array. 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, errpos) => {
  //> err = [ ,'error', ]
  //> res = [1, 2, 3]
  //> errpos = [1]
})

(static) parallelLimit(limit, tasks, 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)

callback function <optional>

optional callback function called by last terminating function from tasks, needs to be of type function (errors: Array<Error>, result: Array<any>, errpos: Array<Number>) where err is either null or an Array containing the errors in the same order as the res results array. 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, errorpos) => {
  //> err = [ ,'error', ]
  //> res = [1, 2, 3]
  //> errorpos = [1]
})

(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