-
Notifications
You must be signed in to change notification settings - Fork 1
/
reduce.js
33 lines (28 loc) · 966 Bytes
/
reduce.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
module.exports = function reduce(array, operator, initial, options) {
var interval = typeof options == 'object' && options.hasOwnProperty('interval')? options.interval: 10;
function recurse(index, initial) {
var startTime = Date.now()
var accumulator = initial
for (var a = index, l = array.length; a < l; a++) {
var item = array[a];
if (Date.now() - startTime > interval) {
return new Promise(function (resolve) {
setImmediate(resolve)
}).then(function () {
return recurse(a, accumulator)
})
} else {
var result = operator(accumulator, item, a)
if (accumulator && typeof accumulator.then === 'function') {
return accumulator.then(function (result) {
return recurse(index + 1, result)
})
} else {
accumulator = result
}
}
}
return Promise.resolve(accumulator)
}
return recurse(0, initial)
}