MCPcopy Create free account
hub / github.com/ctrlplusb/react-tree-walker / pReduce

Function pReduce

src/index.js:16–38  ·  view source on GitHub ↗
(iterable, reducer, initVal)

Source from the content-addressed store, hash-verified

14// Lifted from https://github.com/sindresorhus/p-reduce
15// Thanks @sindresorhus! 🙏
16const pReduce = (iterable, reducer, initVal) =>
17 new Promise((resolve, reject) => {
18 const iterator = iterable[Symbol.iterator]()
19 let i = 0
20
21 const next = total => {
22 const el = iterator.next()
23
24 if (el.done) {
25 resolve(total)
26 return
27 }
28
29 Promise.all([total, el.value])
30 .then(value => {
31 // eslint-disable-next-line no-plusplus
32 next(reducer(value[0], value[1], i++))
33 })
34 .catch(reject)
35 }
36
37 next(initVal)
38 })
39
40// Lifted from https://github.com/sindresorhus/p-map-series
41// Thanks @sindresorhus! 🙏

Callers 1

pMapSeriesFunction · 0.85

Calls 1

nextFunction · 0.85

Tested by

no test coverage detected