// Concurrent async map with a concurrency cap. Applies `fn` across `items`, running at // most `limit` at a time, preserving input order. A tiny worker-pool — no deps. // Extracted from WC26 (fetching ~40 match details in parallel without hammering the upstream). // // const details = await mapLimit(matchIds, 6, (id) => fetchMatch(id)); export async function mapLimit( items: T[], limit: number, fn: (item: T, index: number) => Promise, ): Promise { const out: R[] = new Array(items.length); let i = 0; const workers = Array.from({ length: Math.max(1, Math.min(limit, items.length)) }, async () => { while (i < items.length) { const idx = i++; out[idx] = await fn(items[idx], idx); } }); await Promise.all(workers); return out; }