Skill Tracer
Skills › HyperFrames › media-use › concurrency.mjs

concurrency.mjs

14 lines · skills/media-use/audio/scripts/lib/concurrency.mjs at a595755

1// mapWithConcurrency — run `fn` over `items` with at most `limit` in flight at
2// once. Preserves input order in the result array regardless of completion order.
3export async function mapWithConcurrency(items, limit, fn) {
4 const results = new Array(items.length);
5 let next = 0;
6 async function worker() {
7 while (next < items.length) {
8 const i = next++;
9 results[i] = await fn(items[i], i);
10 }
11 }
12 await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker));
13 return results;