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
|
- 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
|
- 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
})
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 |
|
test |
function | test function |
|
callback |
function |
<optional> |
optional 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 |
|
test |
function | test function |
|
callback |
function |
<optional> |
optional 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 |
|
callback |
function |
<optional> |
optional callback |
- 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. Properties
|
||||||||||||||||
task |
function | iterator function of type |
||||||||||||||||
callback |
function |
<optional> |
optional callback |
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 |
|
callback |
function |
<optional> |
optional callback function called by last
terminating function from |
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 Properties
|
||||||||||||||||
task |
function | iterator function of type |
||||||||||||||||
callback |
function |
<optional> |
optional callback |
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 |
|
task |
function | iterator function of type |
|
callback |
function |
<optional> |
optional callback |
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 |
|
task |
function | iterator function of type |
|
callback |
function |
<optional> |
optional callback |
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]
}
)