( items: readonly T[], limit: number, fn: (item: T, index: number) => Promise<R> )
| 11 | * allSettled-style variant rather than letting a throwing mapper through here. |
| 12 | */ |
| 13 | export async function mapWithConcurrency<T, R>( |
| 14 | items: readonly T[], |
| 15 | limit: number, |
| 16 | fn: (item: T, index: number) => Promise<R> |
| 17 | ): Promise<R[]> { |
| 18 | const results = new Array<R>(items.length) |
| 19 | const workerCount = Math.max(1, Math.min(limit, items.length)) |
| 20 | let cursor = 0 |
| 21 | |
| 22 | const worker = async (): Promise<void> => { |
| 23 | while (true) { |
| 24 | const index = cursor++ |
| 25 | if (index >= items.length) return |
| 26 | results[index] = await fn(items[index], index) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | await Promise.all(Array.from({ length: workerCount }, worker)) |
| 31 | return results |
| 32 | } |
| 33 | |
| 34 | /** Default bound for per-row object-storage materialization fan-out. */ |
| 35 | export const MATERIALIZE_CONCURRENCY = 20 |
no outgoing calls
no test coverage detected