Skip to content

parallel

Runs an array of async functions with a concurrency limit. At most limit functions will be running at any time.

Available since v2.0.0

import { parallel } from '@helpers4/promise';
parallel<T>(functions: readonly function[], limit: number): Promise<T[]>
ParameterTypeDescription
functionsreadonly function[]Array of functions that return promises
limitnumberMaximum number of concurrent executions

Promise<T[]> — Promise that resolves with an array of results in the same order as the input

Executes async functions with at most N running concurrently.

const results = await parallel(
  [() => fetch('/a'), () => fetch('/b'), () => fetch('/c')],
  2
)
// At most 2 requests run at a time; results are in order

Setting limit to 1 runs functions one at a time.

await parallel([fnA, fnB, fnC], 1)
// Runs fnA, then fnB, then fnC

View source on GitHub