UNPKG

@altano/tiny-async-pool

Version:

Run multiple promise-returning & async functions with limited concurrency using native ES9

24 lines (23 loc) 732 B
//#region src/doWorkAndYield.ts /** * Process items from `iterable` in batches and yield the result of each call to * `iteratorFn` */ async function* doWorkAndYield(concurrentCount, iterable, iteratorFn) { const executing = new Set(); async function consume() { const [promise, value] = await Promise.race(executing); executing.delete(promise); return value; } for (const item of iterable) { const result = iteratorFn(item, iterable); const promise = result.then((value) => [promise, value]); executing.add(promise); if (executing.size >= concurrentCount) yield await consume(); } while (executing.size) yield await consume(); } //#endregion export { doWorkAndYield }; //# sourceMappingURL=doWorkAndYield.js.map