-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy patharrayForEachSeries.js
48 lines (45 loc) · 1.15 KB
/
arrayForEachSeries.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const isPromise = require('./isPromise')
const thunkify3 = require('./thunkify3')
// _arrayForEachSeriesAsync(
// array Array,
// callback function,
// index number
// ) -> Promise<array>
const _arrayForEachSeriesAsync = async function (array, callback, index) {
const length = array.length
while (++index < length) {
const operation = callback(array[index])
if (isPromise(operation)) {
await operation
}
}
return array
}
/**
* @name arrayForEachSeries
*
* @synopsis
* ```coffeescript [specscript]
* var T any,
* array Array<T>,
* callback T=>()
*
* arrayForEachSeries(array Array, callback function) -> array|Promise
* ```
*
* @description
* Call a callback for each item of an array in series. Return a promise if any executions are asynchronous.
*/
const arrayForEachSeries = function (array, callback) {
const length = array.length
let index = -1
while (++index < length) {
const operation = callback(array[index])
if (isPromise(operation)) {
return operation
.then(thunkify3(_arrayForEachSeriesAsync, array, callback, index))
}
}
return array
}
module.exports = arrayForEachSeries