Open
Description
Create a parallel function to run async process in parallel. Function parallel will receive three parameters:
- The array to be mapped.
- The async function that will run in each index of the array.
- The max number of parallel process that could be run. Will run all the array parallel if not set.
Ref:
Async map with limited parallelism in Node.js / Article Code
Example:
const users = [{ id: 1, name: 'test' }, { id: 2, name: 'test' }, { id: 3, name: 'test' }]
const fn = async (user, index) => {
// make an http call to other API based on user id
const orders = fetchUserOrders(user.id)
// do something with orders
}
parallel(users, fn) // Will run all the array parallel
parallel(users, fn, 1) // Will wait the function finish and go to next index of the array
parallel(users, fn, 2) // Will run two index of the array in parallel