Tweakr
    Preparing search index...

    Function queue

    • Creates an async task queue with controlled concurrency.

      Tasks are executed in FIFO order, respecting the concurrency limit. Each add call returns a promise that resolves/rejects with the task result.

      Parameters

      • concurrency: number = 1

        Maximum number of tasks running at once (default: 1).

      Returns {
          add: <T>(task: () => Promise<T>) => Promise<T>;
          running: () => number;
          size: () => number;
      }

      An object with methods to add tasks and inspect queue state.

      • add: <T>(task: () => Promise<T>) => Promise<T>

        Add a new async task to the queue.

      • running: () => number

        Returns the number of currently active/running tasks.

      • size: () => number

        Returns the number of pending tasks in the queue.

      const q = queue(2);

      for (let i = 0; i < 5; i++) {
      q.add(async () => {
      console.log("Task", i, "started");
      await new Promise(r => setTimeout(r, 100));
      console.log("Task", i, "done");
      return i * 2;
      }).then(result => console.log("Result:", result));
      }

      1.2.0