()
| 38 | |
| 39 | /// A worker that continuously processes items until there are none left. |
| 40 | const worker = async () => { |
| 41 | while (!firstError && !cancellationToken.isCancellationRequested) { |
| 42 | // Grab the index of the next item. |
| 43 | const index = nextIndex++; |
| 44 | if (index >= items.length) |
| 45 | return; // All items are done. |
| 46 | |
| 47 | const item = items[index]; |
| 48 | onStarted?.(item); // Signal that we're starting. |
| 49 | |
| 50 | try { |
| 51 | await task(item); // Run the main task. |
| 52 | completed++; |
| 53 | onCompleted?.(completed, items.length, item); // Signal that we're done. |
| 54 | } catch (error) { |
| 55 | firstError ??= error; |
| 56 | return; |
| 57 | } |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | await Promise.all(Array.from({ length: maxWorkers }, () => worker())); |
| 62 |
no outgoing calls
no test coverage detected