serial

Serial execution patterns

Source:

Methods

(static) compose(tasks) → {function}

Run composed tasks promises in series.

Result from a task is passed on to the next task. Stops on errors and immediatelly calls next .catch() in this case.

Parameters:
Name Type Description
tasks function | Array

Arguments or Array of functions of type (arg: any) => Promise where

  • arg - an argument which is passed from one task to the other
Source:
Returns:

composed function of (arg: any) => Promise where arg - initial argument which is passed from one task to the other.

Type
function
Example
var c = compose(
 (res) => Promise.resolve(res + 1),
 (res) => Promise.resolve(res + 1),
 (res) => Promise.resolve(res + 1)
)
c(2).then((res) => {
 //> res = 5
})

(static) doUntil(task, test) → {Promise}

Run task one or more times until test returns true. Calls next .catch() at the first error encountered.

Parameters:
Name Type Description
task function

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

test function

test function (index: number) => Boolean. If return value is true then promise gets resolved

Source:
Returns:
Type
Promise
Example
doUntil(
  (i) => Promise.resolve(i),
  (i) => i > 5
).then((res) => {
  console.log(res)
  //> 5
})

(static) doWhilst(task, test) → {Promise}

Run task one or more times until test returns false. Calls next .catch() at the first error encountered.

Parameters:
Name Type Description
task function

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

test function

test function (index: number) => Boolean. If return value is false then promise gets resolved

Source:
Returns:
Type
Promise
Example
doWhilst(
  (i) => Promise.resolve(i),
  (i) => i <= 5
).then((res) => {
  console.log(res)
  //> 5
})

(static) eachSeries(items, task) → {Promise}

Run items on async task function in series. Stops at the first error encountered.

Parameters:
Name Type Description
items Array.<any>

Array of items

task function

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

Source:
Returns:

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

  • results: Array<Any> returns the successfull results or undefined
Type
Promise
Examples

without errors

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

with errors

eachSeries([1, 2, 3, 4],
  (item, index) => (
    new Promise((resolve, reject) => {
      if (index !== 2) resolve(item + index)
      else reject(new Error('error'))
    })
  ))
  .catch((err) => { //
    console.log(err)
    //> { Error: error
    //>   results: [ 1, 3, undefined ]
    //> }
  })

(static) retry(times, task) → {Promise}

Run task max. times times. Stops at first iteration where no error is returned.

Calls next .then() if times is reached or task returned no error, otherwise next .catch().

Parameters:
Name Type Description
times Number | Object

retry max. times times - default=2

Properties
Name Type Attributes Default Description
times Number <optional>
2

max. number of retries

lag Number <optional>
0

time-lag in ms between retries

task function

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

Source:
Returns:
Type
Promise
Example
retry({times: 3, lag: 100}, // max. 3 retries with 100ms time-lag between retries
  (index) => new Promise((resolve, reject) => {
    let err = index < 2 ? new Error() : null
    if (err) reject(err)
    else resolve(index)
  })
).then((res) => {
  //> res = 2
})

(static) series(tasks) → {Promise}

Run tasks functions which return a promise in series The function breaks after the first error encountered

Can process huge number of tasks.

Parameters:
Name Type Description
tasks Array

Array of functions which return a Promise

Source:
Returns:

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

  • results: Array<Any> returns the successfull results or undefined
Type
Promise
Examples

without errors

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

with errors

series([
  () => Promise.resolve(1),
  () => Promise.resolve(2),
  () => Promise.reject(new Error(3)), // breaks on first error
  () => Promise.resolve(4)
]).catch((err) => { //
  console.log(err)
  //> { Error: 3
  //>   results: [ 1, 2, undefined ]
  //> }
})

(static) times(times, task) → {Promise}

Run task repeatedly until number times is reached.

Stops at the first error encountered. An optional lag between retries may be used.

Parameters:
Name Type Description
times Number | Object

runs times times. If times < 0 then "times" cycles endlessly until an error occurs.

Properties
Name Type Attributes Default Description
times Number <optional>
0

max. number of retries

lag Number <optional>
0

time-lag in ms between retries

task function

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

Source:
Returns:
Type
Promise
Example
let arr = []
times({times: 4, lag: 100}, // 4 times with 100ms time-lag between retries
  (index) => new Promise((resolve) => {
    arr.push(index)
    resolve(index)
  })
).then((res) => {
  //> res = 3
  //> arr = [0, 1, 2, 3]
  assert.equal(res, 3)
  assert.deepEqual(arr, [0, 1, 2, 3])
})

(static) until(test, task) → {Promise}

Run task repeatedly until test returns true. Calls next .catch() at the first error encountered.

Parameters:
Name Type Description
test function

test function (index: number) => Boolean. If return value is true then promise gets resolved

task function

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

Source:
Returns:
Type
Promise
Example
let arr = []
until(
  (index) => (index >= 4),
  (index) => new Promise((resolve) => {
    arr.push(index)
    resolve(index)
  })
).then((res) => {
  //> res = 3
  //> arr = [0, 1, 2, 3]
})

(static) whilst(test, task) → {Promise}

Run task repeatedly until test returns false. Calls next .catch() at the first error encountered.

Parameters:
Name Type Description
test function

test function (index: number) => Boolean. If return value is false then promise gets resolved

task function

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

Source:
Returns:
Type
Promise
Example
let arr = []
whilst(
  (index) => (index < 4),
  (index) => new Promise((resolve) => {
    arr.push(index)
    resolve(index)
  })
).then((res) => {
  //> res = 3
  //> arr = [0, 1, 2, 3]
})