@pengzhanbo/utils - v3.12.0
    Preparing search index...

    Function promiseParallel

    • Executes an array of promises in parallel with a given concurrency. The function returns a Promise that resolves with an array containing the resolved values of each promise. If any promise is rejected, the returned promise will be rejected.

      以指定的并发数并行执行一组 promise。该函数返回一个 promise,该 promise 解析为一个数组,包含每个 promise 的解析值。 任意一个 promise 拒绝,返回的 promise 将被拒绝。

      Type Parameters

      • T

      Parameters

      • promises: (PromiseLike<T> | (() => PromiseLike<T>))[]

        The array of promises or promise-returning functions to execute. 要执行的promise数组或返回promise的函数数组

      • concurrency: number = Number.POSITIVE_INFINITY

        (optional) The maximum number of promises to execute in parallel. Defaults to infinity. 最大并发数,默认为无穷大

      Returns Promise<Awaited<T>[]>

      A Promise that resolves with an array containing the resolved values of each promise. 一个Promise,它解析为一个包含每个promise解析值的数组

      This function fails fast: as soon as one task rejects, the returned promise rejects and no further tasks are scheduled. Tasks already in flight keep running, but their results are ignored. For a batch that runs every task regardless of failures, use promiseParallelSettled; for limiting the concurrency of a single function across call sites, use limitAsync.

      此函数快速失败:一旦某个任务拒绝,返回的 promise 立即拒绝且不再调度后续任务。 已在运行的任务会继续执行,但其结果会被忽略。如果希望无论失败与否都执行全部任务, 请使用 promiseParallelSettled;如果希望限制单个函数在任意调用点的并发, 请使用 limitAsync

      const promises = [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)]
      const result = await promiseParallel(promises, 2) // maximum concurrency of 2 - 最大并发数为2
      console.log(result) // [1, 2, 3]
      • promiseParallelSettled — for running every task regardless of failures / 无论成败都执行全部任务
      • limitAsync — for limiting a single function's concurrency / 限制单个函数的并发