(items: T[], limit: number, fn: PLimitFn<T>)
| 2 | |
| 3 | /** Runs multiple async functions, with limited concurrency. */ |
| 4 | export async function pLimit<T>(items: T[], limit: number, fn: PLimitFn<T>): Promise<void> { |
| 5 | // Iterate values in order, 0–N. |
| 6 | items = items.slice().reverse(); |
| 7 | |
| 8 | let index = 0; |
| 9 | |
| 10 | // Spawn 'limit' concurrent functions. |
| 11 | await Promise.all( |
| 12 | [...Array(limit)].map(async () => { |
| 13 | while (items.length > 0) { |
| 14 | await fn(items.pop()!, index++); |
| 15 | } |
| 16 | }), |
| 17 | ); |
| 18 | } |
no outgoing calls
no test coverage detected