ipull
Version:
The only file downloader you'll ever need. For node.js and the browser, CLI and library for fast and reliable file downloads.
26 lines • 747 B
JavaScript
import { promiseWithResolvers } from "./promiseWithResolvers.js";
export function concurrency(array, concurrencyCount, callback) {
const { resolve, reject, promise } = promiseWithResolvers();
let index = 0;
let activeCount = 0;
function reload() {
if (index === array.length && activeCount === 0) {
resolve();
return;
}
while (activeCount < concurrencyCount && index < array.length) {
activeCount++;
callback(array[index++])
.then(() => {
activeCount--;
reload();
}, reject);
}
}
reload();
return {
promise,
reload
};
}
//# sourceMappingURL=concurrency.js.map