Tweakr
    Preparing search index...

    Function parallelLimit

    • Executes asynchronous tasks in parallel with a concurrency limit.

      Tasks are executed up to limit at a time. Results are returned in the same order as the original task array.

      Type Parameters

      • T

        The type of each resolved result.

      Parameters

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

        An array of async task functions returning promises.

      • limit: number = 5

        Maximum number of concurrent tasks (default: 5).

      Returns Promise<T[]>

      A promise resolving to an array of results in original order.

      const tasks = Array.from({ length: 10 }, (_, i) => async () => {
      await new Promise(r => setTimeout(r, 100));
      return i;
      });

      const results = await parallelLimit(tasks, 3);
      console.log(results); // [0, 1, 2, 3, ...]

      If any task rejects, the error is propagated immediately.

      1.2.0