← Files JuicyLucy AdsARCHIVED FILE

skills/media-use/audio/scripts/lib/concurrency.mjs

527 Bytes · Oct 4, 2026 · 12:34 UTC

↓ Download file

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

SHA-256: 36a28dc1a0528b5605cba29a593dd54a786ad6283611b09a634946b12a5ed32e