(tasks: (() => Promise<T>)[], limit: number)
| 2 | * Helper: Run tasks in parallel with concurrency limit |
| 3 | */ |
| 4 | export async function parallelLimit<T>(tasks: (() => Promise<T>)[], limit: number): Promise<T[]> { |
| 5 | const results: T[] = []; |
| 6 | const executing: Promise<void>[] = []; |
| 7 | |
| 8 | for (const task of tasks) { |
| 9 | const p = task().then((result) => { |
| 10 | results.push(result); |
| 11 | executing.splice(executing.indexOf(p), 1); |
| 12 | }); |
| 13 | executing.push(p); |
| 14 | |
| 15 | if (executing.length >= limit) { |
| 16 | await Promise.race(executing); |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | await Promise.all(executing); |
| 21 | return results; |
| 22 | } |
no test coverage detected