export async function mapWithConcurrency(items: readonly T[], concurrency: number, worker: (item: T, index: number) => Promise): Promise { const limit = Math.max(1, Math.floor(concurrency)); let nextIndex = 0; const runners = Array.from({ length: Math.min(limit, items.length) }, async () => { while (true) { const index = nextIndex; nextIndex += 1; if (index >= items.length) { return; } await worker(items[index] as T, index); } }); await Promise.all(runners); }