rubico
Version:
[a]synchronous functional programming
31 lines (28 loc) • 684 B
JavaScript
/**
* @name asyncIteratorReduce
*
* @synopsis
* ```coffeescript [specscript]
* asyncIteratorReduce(
* asyncIterator AsyncIterator<T>,
* reducer (any, T)=>Promise|any,
* result any,
* ) -> result any
* ```
*/
const asyncIteratorReduce = async function (asyncIterator, reducer, result) {
let iteration = await asyncIterator.next()
if (iteration.done) {
return result
}
if (result === undefined) {
result = iteration.value
iteration = await asyncIterator.next()
}
while (!iteration.done) {
result = await reducer(result, iteration.value)
iteration = await asyncIterator.next()
}
return result
}
module.exports = asyncIteratorReduce