mirror of
https://github.com/rxaviers/async-pool.git
synced 2026-01-25 16:07:54 +00:00
* Remove test that fails after commit #3114536 * Fix prettier command * Improve tests for error handling Fixes #9
45 lines
1.1 KiB
JavaScript
45 lines
1.1 KiB
JavaScript
let assert, assertType;
|
|
const shouldAssert = process.env.NODE_ENV === "development";
|
|
|
|
if (shouldAssert) {
|
|
({ assert, assertType } = require("yaassertion"));
|
|
}
|
|
|
|
function asyncPool(poolLimit, array, iteratorFn) {
|
|
if (shouldAssert) {
|
|
try {
|
|
assertType(poolLimit, "poolLimit", ["number"]);
|
|
assertType(array, "array", ["array"]);
|
|
assertType(iteratorFn, "iteratorFn", ["function"]);
|
|
} catch (error) {
|
|
return Promise.reject(error);
|
|
}
|
|
}
|
|
let i = 0;
|
|
const ret = [];
|
|
const executing = [];
|
|
const enqueue = function() {
|
|
if (i === array.length) {
|
|
return Promise.resolve();
|
|
}
|
|
const item = array[i++];
|
|
const p = Promise.resolve().then(() => iteratorFn(item, array));
|
|
ret.push(p);
|
|
|
|
let r = Promise.resolve();
|
|
|
|
if (poolLimit <= array.length) {
|
|
const e = p.then(() => executing.splice(executing.indexOf(e), 1));
|
|
executing.push(e);
|
|
if (executing.length >= poolLimit) {
|
|
r = Promise.race(executing);
|
|
}
|
|
}
|
|
|
|
return r.then(() => enqueue());
|
|
};
|
|
return enqueue().then(() => Promise.all(ret));
|
|
}
|
|
|
|
module.exports = asyncPool;
|