parallel
Parallelize async operations while managing load
Usage
The parallel
function processes an array with an async callback. The first argument controls how many array items are processed at one time. Similar to Promise.all
, an ordered array of results is returned.
Since v12.3.0, if the limit is greater than the array length, it will be clamped to the array length. Similarly, if the limit is less than 1, it will be clamped to 1.
Interrupting
Since v12.3.0, processing can be manually interrupted. Pass an AbortController.signal
via the signal
option. When the signal is aborted, no more calls to your callback will be made. Any in-progress calls will continue to completion, unless you manually connect the signal inside your callback. In other words, parallel
is only responsible for aborting the array loop, not the async operations themselves.
When parallel
is interrupted by the signal, it throws a DOMException
(even in Node.js) with the message This operation was aborted
and name AbortError
.
Aggregate Errors
Once the whole array has been processed, parallel
will check for errors. If any errors occurred during processing, they are combined into a single AggregateError
. The AggregateError
has an errors
array property which contains all the individual errors that were thrown.