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
|
- 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 |
test |
function | test function |
- 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 |
test |
function | test function |
- 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 |
- 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
eachSeries([1, 2, 3, 4],
(item, index) => (
new Promise((resolve, reject) => {
resolve(item + index)
})
))
.then((results) => {
console.log(results)
//> [1, 3, 5, 7]
})
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. Properties
|
|||||||||||||||
task |
function | iterator function of type |
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 |
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
series([
() => Promise.resolve(1),
() => Promise.resolve(2),
() => Promise.resolve(3)
]).then((results) => {
console.log(results)
//> [1, 2, 3]
})
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 Properties
|
|||||||||||||||
task |
function | iterator function of type |
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 |
task |
function | iterator function of type |
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 |
task |
function | iterator function of type |
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]
})