parallel

Parallel execution patterns

Source:

Methods

(static) each(items, task, optionsopt) → {Promise}

Run items on async task promise in parallel.

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

Parameters:
Name Type Attributes Description
items Array.<any>

Array of items

task function

iterator function of type (item: any, index: Number) => Promise

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

Source:
Returns:

on resolve .then(results: Array<any> => {}) and on reject .catch(error: AsynccError => {}) where error is the first thrown error containing the properties:

  • errors: Array<Error> list of errors
  • errpos: Array<Number> gives the positions of errors in order as they occur.
  • results: Array<Any> returns the successfull results or undefined
Type
Promise
Examples

without errors

each([1, 2, 3],
  (item, index) => (
    new Promise((resolve, reject) => {
      resolve(item + index)
  }))
)
.then((results) => {
  console.log(results)
  //> [1, 3, 5]
})

with errors

each([1, 2, 3],
  (item, index) => (
    new Promise((resolve, reject) => {
      if (index % 2) resolve(item + index)
      else reject(new Error('error'))
  }))
)
.catch((err) => { //
  console.log(err)
  //> { AsynccError: err
  //>   errors: [
  //>     Error: error,
  //>     null,
  //>     Error: error
  //>   ],
  //>   errpos: [0, 2],
  //>   results: [undefined, 3, undefined]
  //> }
})

(static) eachLimit(limit, items, task, optionsopt) → {Promise}

Run items on async task promise in parallel limited to limit running in 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.<any>

Array of items

task function

iterator function of type (item: any, index: Number) => Promise

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

Source:
Returns:

on resolve .then(results: Array<any> => {}) and on reject .catch(error: AsynccError => {}) where error is the first thrown error containing the properties:

  • errors: Array<Error> list of errors
  • errpos: Array<Number> gives the positions of errors in order as they occur.
  • results: Array<Any> returns the successfull results or undefined
Type
Promise
Examples

without errors

eachLimit(2, [1, 2, 3, 4],
 (item, index) => (
   new Promise((resolve, reject) => {
     resolve(item + index)
   }))
)
.then((results) => {
  console.log(results)
  //> [1, 3, 5, 7]
})

with errors

eachLimit(2, [1, 2, 3, 4],
 (item, index) => (
   new Promise((resolve, reject) => {
     if (index % 2) resolve(item + index)
     else reject(new TypeError('error'))
 }))
)
.catch((err) => { //
 console.log(err)
 //> { TypeError: error
 //>   errors: [[Circular], null, TypeError: error, null],
 //>   errpos: [0, 2],
 //>   results: [undefined, 3, undefined, 7]
 //> }
})

(static) parallel(tasks, optionsopt) → {Promise}

Run tasks returning Promises in parallel.

Does not stop parallel execution on errors. All tasks get executed. then() gets called after the longest running task finishes.

If bail-out on first error is desired, consider Promise.all() as an alternative, or set {bail: true} as option.

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

Array of functions of type () => Promise

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

Source:
Returns:

on resolve .then(results: Array<any> => {}) and on reject .catch(error: AsynccError => {}) where error is the first thrown error containing the properties:

  • errors: Array<Error> list of errors
  • errpos: Array<Number> gives the positions of errors in order as they occur.
  • results: Array<Any> returns the successfull results or undefined
Type
Promise
Examples

without errors

parallel([
  () => Promise.resolve(1),
  () => Promise.resolve(3),
  () => Promise.resolve(5)
]).then((results) => {
  console.log(results)
  //> [1, 3, 5]
})

with errors and timeout = 100ms

parallel([
  () => Promise.reject(new Error(1)),
  () => Promise.resolve(3),
  () => Promise.reject(new Error(5))
], {timeout: 100}).then((result) => { // won't reach here
}).catch((err) => {
  console.log(err)
  //> { AsynccError:
  //>   errors: [Error: 1, null, Error: 5],
  //>   errpos: [0, 2],
  //>   results: [undefined, 3, undefined]
  //> }
})

(static) parallelLimit(limit, tasks, optionsopt) → {Promise}

Run tasks returning Promises in parallel limited to limit parallel running tasks.

Does not stop parallel execution on errors. All tasks get executed. then() gets called after the longest running task finishes.

If bail-out on first error is desired, consider set {bail: true} as option.

Parameters:
Name Type Attributes Description
limit Number

number of tasks running in parallel

tasks Array.<function()>

Array of functions of type () => Promise

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

Source:
Returns:

on resolve .then(results: Array<any> => {}) and on reject .catch(error: AsynccError => {}) where error is the first thrown error containing the properties:

  • errors: Array<Error> list of errors
  • errpos: Array<Number> gives the positions of errors in order as they occur.
  • results: Array<Any> returns the successfull results or undefined
Type
Promise
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]
})