UNPKG

rubico

Version:

[a]synchronous functional programming

33 lines (30 loc) 824 B
const isPromise = require('./isPromise') const promiseAll = require('./promiseAll') const always = require('./always') /** * @name asyncIteratorForEach * * @synopsis * ```coffeescript [specscript] * var T any, * asyncIterator AsyncIterator<T>, * callback T=>() * * asyncIteratorForEach(asyncIterator, callback) -> Promise<> * ``` * * @description * Execute a callback function for each item of an async iterator */ const asyncIteratorForEach = async function (asyncIterator, callback) { const promises = [] for await (const item of asyncIterator) { const operation = callback(item) if (isPromise(operation)) { promises.push(operation) } } return promises.length == 0 ? asyncIterator : promiseAll(promises).then(always(asyncIterator)) } module.exports = asyncIteratorForEach