mirror of
https://github.com/rxaviers/async-pool.git
synced 2026-01-18 16:04:07 +00:00
26 lines
763 B
JavaScript
26 lines
763 B
JavaScript
async function* asyncPool(concurrency, 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) {
|
|
// Wrap iteratorFn() in an async fn to ensure we get a promise.
|
|
// Then expose such promise, so it's possible to later reference and
|
|
// remove it from the executing pool.
|
|
const promise = (async () => await iteratorFn(item, iterable))().then(
|
|
value => [promise, value]
|
|
);
|
|
executing.add(promise);
|
|
if (executing.size >= concurrency) {
|
|
yield await consume();
|
|
}
|
|
}
|
|
while (executing.size) {
|
|
yield await consume();
|
|
}
|
|
}
|
|
|
|
module.exports = asyncPool;
|