1
This commit is contained in:
27
server/services/flows/flow_utils.js
Normal file
27
server/services/flows/flow_utils.js
Normal file
@@ -0,0 +1,27 @@
|
||||
export async function sleep_ms(ms) {
|
||||
await new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
export async function map_limit(items, limit, worker) {
|
||||
const list = Array.isArray(items) ? items : [];
|
||||
const n = Math.max(1, Number(limit || 1));
|
||||
|
||||
const res = new Array(list.length);
|
||||
let idx = 0;
|
||||
|
||||
async function run_one() {
|
||||
while (idx < list.length) {
|
||||
const cur = idx;
|
||||
idx += 1;
|
||||
res[cur] = await worker(list[cur], cur);
|
||||
}
|
||||
}
|
||||
|
||||
const runners = [];
|
||||
for (let i = 0; i < Math.min(n, list.length); i += 1) {
|
||||
runners.push(run_one());
|
||||
}
|
||||
|
||||
await Promise.all(runners);
|
||||
return res;
|
||||
}
|
||||
Reference in New Issue
Block a user