export async function runWithConcurrency( items: T[], limit: number, worker: (item: T) => Promise, ) { let nextIndex = 0; const workerCount = Math.min(Math.max(1, limit), items.length); await Promise.all( Array.from({ length: workerCount }, async () => { while (nextIndex < items.length) { const index = nextIndex; nextIndex += 1; await worker(items[index]); } }), ); }