This commit is contained in:
2026-04-02 17:50:10 +08:00
parent 60697d80be
commit ce0aa4f789
373 changed files with 82520 additions and 46 deletions

View File

@@ -0,0 +1,37 @@
'use strict';
module.exports = parallelLimit;
/*!
* ignore
*/
async function parallelLimit(params, fn, limit) {
if (limit <= 0) {
throw new Error('Limit must be positive');
}
if (params.length === 0) {
return [];
}
const results = [];
const executing = new Set();
for (let index = 0; index < params.length; index++) {
const param = params[index];
const p = fn(param, index);
results.push(p);
executing.add(p);
const clean = () => executing.delete(p);
p.then(clean).catch(clean);
if (executing.size >= limit) {
await Promise.race(executing);
}
}
return Promise.all(results);
}