serial

Serial execution patterns

Source:

Methods

(static) compose(tasks) → {function}

Run composed tasks callback functions in series. Results from a task are passed no the next task. Stops on errors and immediatelly calls optional callback in this case.

Parameters:
Name Type Description
tasks function | Array

Arguments or Array of callback functions of type function (arg: any, cb: function) arg - an argument which is passed from one task to the other cb - the callback function which needs to be called on completion

Source:
Returns:

composed function of function (arg: any, cb: function) where arg - initial argument which is passed from one task to the other [callback] - optional callback function(err: <Error>, res: any)

Type
function
Example
var c = compose(
  (res, cb) => { cb(null, res + 1) },
  (res, cb) => { cb('error', res * 2) }, // breaks here on first error
  (res, cb) => { cb(null, res + 3) },
)
c(2, function (err, res) {
  //> err = 'error'
  //> res = 6
})

(static) connect(tasks) → {function}

Run composed tasks callback functions in series. Results from a task are passed to the next task. Passed or thrown errors in tasks get trapped with functions of arity 3 function (err, res, cb) called here trap. In case that there is no previous error, a trap acts as "no-op". In case that no trap is defined then the chain exits to an optional callback.

Parameters:
Name Type Description
tasks function | Array

Arguments or Array of callback functions of type task function (arg: any, cb: function) or trap function (err: <Error>, arg: any, cb: function) where arg - an argument which is passed from one task to the other err - a trapped error from previous tasks cb - the callback function which needs to be called on completion

Source:
Returns:

composed function of function (arg, cb) where arg - initial argument which is passed from one task to the other [callback] - optional callback function function(err: <Error>, res: any)

Type
function
Examples
var c = connect(
  (res, cb) => { cb(null, res + 1) },      // task
  (err, res, cb) => { cb(null, res + 3) }, // trap - "no-op" here as there is no previous error
  (res, cb) => { cb(null, res * 2) }       // task
)
c(2, function (err, res) {
  //> err = null
  //> res = 6
})

With error traps

var c = connect(
  (res, cb) => { cb('error', res + 1) },   // task - error is passed to next task
  (res, cb) => { cb(null, res * 2) },      // task - "no-op", jumps over this task due to previous error
  (err, res, cb) => { cb(null, res + 3) }, // trap - error gets trapped here (arity === 3)
  (res, cb) => { cb(null, res * 2) }       // task - continues
)
c(2, function (err, res) {
  //> err = null
  //> res = 12
})

(static) doUntil(task, test, callbackopt)

Run task one or more times until test returns true. Calls callback at the first error encountered.

Parameters:
Name Type Attributes Description
task function

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

test function

test function function (index: number). If return value is true then callback gets called

callback function <optional>

optional callback function (errors: <Error>, result: any) from last callback.

Source:
Example
let arr = []
doUntil(
  (cb, index) => {    // task
    arr.push(index)
    cb(null, index)
  }, (index) => {     // test
    return index >= 4
  }, (err, res) => {  // callback
    //> err = null
    //> res = 3
    //> arr = [0, 1, 2, 3]
  }
)

(static) doWhilst(task, test, callbackopt)

Run task one or more times until test returns false. Calls callback at the first error encountered.

Parameters:
Name Type Attributes Description
task function

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

test function

test function function (index: number). If return value is false then callback gets called

callback function <optional>

optional callback function (errors: <Error>, result: any) from last callback.

Source:
Example
let arr = []
doWhilst(
  (cb, index) => {    // task
    arr.push(index)
    cb(null, index)
  }, (index) => {     // test
    return index < 4
  }, (err, res) => {  // callback
    //> err = null
    //> res = 3
    //> arr = [0, 1, 2, 3]
  }
)

(static) eachSeries(items, task, callbackopt)

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

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

Array of items

task function

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

callback function <optional>

optional callback function (errors: <Error>, result: Array<any>)

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

(static) noPromise(arg) → {NoPromise}

This is not a Promise.

Chain callback functions with .then(function (res, cb)) and execute them as soon as previous callbacks have finished.

Catch passed or thrown errors with .catch(function (err, res, cb)) as they may occur. End the chain with .end(function (err, res)).

If errors are thrown inside a task they are catched and can be processed attaching .catch() or .end() to the chain.

See full API here NoPromise.

Parameters:
Name Type Description
arg Any

initial argument which is passed to first chain

Source:
Returns:
Type
NoPromise

(static) retry(times, task, callbackopt)

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

Calls callback if times is reached or task returned no error.

Parameters:
Name Type Attributes 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 function (cb: Function, index: Number)

callback function <optional>

optional callback function (errors: Error, result: any) from last callback.

Source:
Example
let arr = []
retry({times: 3, lag: 100}, // max. 3 retries with 100ms time-lag between retries
  (cb, index) => { // task
    let err = index < 2 ? new Error() : null
    arr.push(index)
    cb(err, index)
  }, (err, res) => { // callback
    //> err = null
    //> res = 2
    //> arr = [0, 1, 2]
  }
)

(static) series(tasks, callbackopt)

Run tasks callback functions in series The function breaks after the first error encountered and calls optional callback function

Parameters:
Name Type Attributes Description
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 (err: Error, res: Array<any>)

Source:
Example
series([
  (cb) => { cb(null, 1) },
  (cb) => { cb('error', 2) }, // breaks on first error
  (cb) => { cb(null, 3) },
], (err, res) => {
  //> err = 'error'
  //> res = [1, 2]
})

(static) times(times, task, callbackopt)

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 Attributes 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 function (cb: Function, index: Number)

callback function <optional>

optional callback function (errors: Error, result: Array<any>)

Source:
Example
let arr = []
times({times: 4, lag: 100}, // 4 times with 100ms time-lag between retries
  (cb, index) => {
    arr.push(index)
    cb(null, index)
  }, (err, res) => {
    //> err = null
    //> res = 3
    //> arr = [0, 1, 2, 3]
  }
)

(static) until(test, task, callbackopt)

Run task repeatedly until test returns true. Calls callback at the first error encountered.

Parameters:
Name Type Attributes Description
test function

test function function (index: number). If return value is true then callback gets called

task function

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

callback function <optional>

optional callback function (errors: <Error>, result: any) from last callback.

Source:
Example
let arr = []
until(
  (index) => {        // test
    return index >= 4
  }, (cb, index) => { // task
    arr.push(index)
    cb(null, index)
  }, (err, res) => {  // callback
    //> err = null
    //> res = 3
    //> arr = [0, 1, 2, 3]
  }
)

(static) whilst(test, task, callbackopt)

Run task repeatedly until test returns false. Calls callback at the first error encountered.

Parameters:
Name Type Attributes Description
test function

test function function (index: number). If return value is false then callback gets called

task function

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

callback function <optional>

optional callback function (errors: Error, result: any) from last callback.

Source:
Example
let arr = []
whilst(
  (index) => (index < 4), // test
  (cb, index) => { // task
    arr.push(index)
    cb(null, index)
  }, (err, res) => { // callback
    //> err = null
    //> res = 3
    //> arr = [0, 1, 2, 3]
  }
)