• Version 7.1.0
  • Published
  • 36.9 kB
  • 2 dependencies
  • MIT license


npm i p-queue
yarn add p-queue
pnpm add p-queue


Promise queue with concurrency control



class PQueue

class PQueue<
QueueType extends Queue<RunFunction, EnqueueOptionsType> = PriorityQueue,
EnqueueOptionsType extends QueueAddOptions = DefaultAddOptions
> extends EventEmitter<'active' | 'idle' | 'add' | 'next' | 'completed' | 'error'> {}
  • Promise queue with concurrency control.


constructor(options?: Options<QueueType, EnqueueOptionsType>);

    property concurrency

    concurrency: number;

      property isPaused

      readonly isPaused: boolean;
      • Whether the queue is currently paused.

      property pending

      readonly pending: number;
      • Number of running items (no longer in the queue).

      property size

      readonly size: number;
      • Size of the queue, the number of queued items waiting to run.

      property timeout

      timeout: number;

        method add

        add: <TaskResultType>(
        fn: Task<TaskResultType>,
        options?: Partial<EnqueueOptionsType>
        ) => Promise<TaskResultType>;
        • Adds a sync or async task to the queue. Always returns a promise.

        method addAll

        addAll: <TaskResultsType>(
        functions: ReadonlyArray<Task<TaskResultsType>>,
        options?: EnqueueOptionsType
        ) => Promise<TaskResultsType[]>;
        • Same as .add(), but accepts an array of sync or async functions.


          A promise that resolves when all functions are resolved.

        method clear

        clear: () => void;
        • Clear the queue.

        method onEmpty

        onEmpty: () => Promise<void>;
        • Can be called multiple times. Useful if you for example add additional items at a later time.


          A promise that settles when the queue becomes empty.

        method onIdle

        onIdle: () => Promise<void>;
        • The difference with .onEmpty is that .onIdle guarantees that all work from the queue has finished. .onEmpty merely signals that the queue is empty, but it could mean that some promises haven't completed yet.


          A promise that settles when the queue becomes empty, and all promises have completed; queue.size === 0 && queue.pending === 0.

        method onSizeLessThan

        onSizeLessThan: (limit: number) => Promise<void>;
        • Returns

          A promise that settles when the queue size is less than the given limit: queue.size < limit.

          If you want to avoid having the queue grow beyond a certain size you can await queue.onSizeLessThan() before adding a new item.

          Note that this only limits the number of items waiting to start. There could still be up to concurrency jobs already running that this call does not include in its calculation.

        method pause

        pause: () => void;
        • Put queue execution on hold.

        method sizeBy

        sizeBy: (options: Readonly<Partial<EnqueueOptionsType>>) => number;
        • Size of the queue, filtered by the given options.

          For example, this can be used to find the number of items remaining in the queue with a specific priority level.

        method start

        start: () => this;
        • Start (or resume) executing enqueued tasks within concurrency limit. No need to call this if queue is not paused (via options.autoStart = false or by .pause() method.)


        interface DefaultAddOptions

        interface DefaultAddOptions extends QueueAddOptions {}

          property priority

          readonly priority?: number;
          • Priority of operation. Operations with greater priority will be scheduled first.


          interface Options

          interface Options<
          QueueType extends Queue<RunFunction, QueueOptions>,
          QueueOptions extends QueueAddOptions
          > {}

            property autoStart

            readonly autoStart?: boolean;
            • Whether queue tasks within concurrency limit, are auto-executed as soon as they're added.


            property carryoverConcurrencyCount

            readonly carryoverConcurrencyCount?: boolean;
            • Whether the task must finish in the given interval or will be carried over into the next interval count.


            property concurrency

            readonly concurrency?: number;
            • Concurrency limit.

              Minimum: 1.


            property interval

            readonly interval?: number;
            • The length of time in milliseconds before the interval count resets. Must be finite.

              Minimum: 0.


            property intervalCap

            readonly intervalCap?: number;
            • The max number of runs in the given interval of time.

              Minimum: 1.


            property queueClass

            readonly queueClass?: new () => QueueType;
            • Class with a enqueue and dequeue method, and a size getter. See the [Custom QueueClass](https://github.com/sindresorhus/p-queue#custom-queueclass) section.

            property throwOnTimeout

            throwOnTimeout?: boolean;
            • Whether or not a timeout is considered an exception.


            property timeout

            timeout?: number;
            • Per-operation timeout in milliseconds. Operations fulfill once timeout elapses if they haven't already.

            interface Queue

            interface Queue<Element, Options> {}

              property dequeue

              dequeue: () => Element | undefined;

                property enqueue

                enqueue: (run: Element, options?: Partial<Options>) => void;

                  property filter

                  filter: (options: Partial<Options>) => Element[];

                    property size

                    size: number;

                      Type Aliases

                      type QueueAddOptions

                      type QueueAddOptions = Readonly<Record<string, unknown>>;

                        Package Files (3)

                        Dependencies (2)

                        Dev Dependencies (15)

                        Peer Dependencies (0)

                        No peer dependencies.


                        To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                        You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/p-queue.

                        • Markdown
                        • HTML
                          <a href="https://www.jsdocs.io/package/p-queue"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>