Function asyncTasksLimit

  • Run async tasks in parallel with a limit on the number of concurrent tasks.

    Type Parameters

    • T

    Parameters

    • concurrency: number

      number of tasks to run concurrently

    • tasks: (() => Promise<any>)[]

      array of functions that return a promise

    • Optional callback: ((value, index) => void)

      callback is invoked in order of completion

        • (value, index): void
        • Parameters

          • value: T
          • index: number

          Returns void

    Returns Promise<T[]>

    array of return values which are ordered with indices identical to their respective tasks in the input array.

    Typeparam

    T - The type of the return value of the tasks.

    Throws

    Will throw an error if any of the tasks fail.

    Example

    const tasks = [
    () => new Promise((resolve) => setTimeout(() => resolve('Task A complete'), 2000)),
    () => new Promise((resolve) => setTimeout(() => resolve('Task B complete'), 1000)),
    () => new Promise((resolve) => setTimeout(() => resolve('Task C complete'), 3000)),
    () => new Promise((resolve) => setTimeout(() => resolve('Task D complete'), 4000)),
    () => new Promise((resolve) => setTimeout(() => resolve('Task E complete'), 5000)),
    ]
    const results2 = await asyncTasksLimit(2, tasks, (value, index) => {
    console.log(`Task ${index} completed with value ${value}`)
    })
    console.log(results2)

Generated using TypeDoc