UNPKG

async-collect

Version:

Keep asynchronously getting chunks of data as long as a specified condition is met.

17 lines (15 loc) 447 B
export default ({ getChunk, getNextCallsArgs, reduce }) => { return collect; async function collect(...args) { const chunk = await getChunk(...args); const nextCallsArgs = getNextCallsArgs(args, chunk); if (nextCallsArgs.length === 0) return chunk; const nextChunks = [ chunk, ...(await Promise.all( nextCallsArgs.map(callArgs => collect(...callArgs)) )) ]; return reduce(nextChunks); } };