Skip to content

parallel

Parallelize async operations while managing load

1698 bytes

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.

import * as _ from 'radashi'
const userIds = [1, 2, 3, 4, 5, 6, 7, 8, 9]
// Will run the find user async function 3 at a time
// starting another request when one of the 3 is freed
const users = await _.parallel(3, userIds, async userId => {
return await api.users.find(userId)
})

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.

import * as _ from 'radashi'
const abortController = new AbortController()
const signal = abortController.signal
// Pass in the signal:
const pizzas = await _.parallel(
{ limit: 2, signal },
['pepperoni', 'cheese', 'mushroom'],
async topping => {
return await bakePizzaInWoodFiredOven(topping) // each pizza takes 10 minutes!
},
)
// Later on, if you need to abort:
abortController.abort()

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.

import * as _ from 'radashi'
const userIds = [1, 2, 3]
const [err, users] = await _.tryit(_.parallel)(3, userIds, async userId => {
throw new Error(`No, I don\'t want to find user ${userId}`)
})
console.log(err) // => AggregateError
console.log(err.errors) // => [Error, Error, Error]
console.log(err.errors[1].message) // => "No, I don't want to find user 2"