export async function mapLimit(items: readonly T[], limit: number, fn: (item: T, index: number) => Promise): Promise { if (items.length === 0) return []; const workerCount = Math.max(1, Math.min(Math.floor(limit), items.length)); const results: R[] = new Array(items.length); let nextIndex = 0; async function worker() { while (nextIndex < items.length) { const index = nextIndex; nextIndex += 1; results[index] = await fn(items[index]!, index); } } await Promise.all(Array.from({ length: workerCount }, () => worker())); return results; }