• Version 8.0.1
  • Published
  • 36 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 = QueueAddOptions
> extends EventEmitter<EventName> {}
  • 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;
      • Per-operation timeout in milliseconds. Operations fulfill once timeout elapses if they haven't already.

        Applies to each future operation.

      method add

      add: {
      function_: Task<TaskResultType>,
      options: { throwOnTimeout: true } & Exclude<
      ): Promise<TaskResultType>;
      function_: Task<TaskResultType>,
      options?: Partial<EnqueueOptionsType>
      ): Promise<void | TaskResultType>;
      • Adds a sync or async task to the queue. Always returns a promise.

      method addAll

      addAll: {
      functions: ReadonlyArray<Task<TaskResultsType>>,
      options?: { throwOnTimeout: true } & Partial<
      Exclude<EnqueueOptionsType, 'throwOnTimeout'>
      ): Promise<TaskResultsType[]>;
      functions: readonly Task<TaskResultsType>[],
      options?: Partial<EnqueueOptionsType>
      ): Promise<(void | 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.)

      Type Aliases

      type Options

      type Options<
      QueueType extends Queue<RunFunction, QueueOptions>,
      QueueOptions extends QueueAddOptions
      > = {
      Concurrency limit.
      Minimum: `1`.
      @default Infinity
      readonly concurrency?: number;
      Whether queue tasks within concurrency limit, are auto-executed as soon as they're added.
      @default true
      readonly autoStart?: boolean;
      Class with a `enqueue` and `dequeue` method, and a `size` getter. See the [Custom QueueClass](https://github.com/sindresorhus/p-queue#custom-queueclass) section.
      readonly queueClass?: new () => QueueType;
      The max number of runs in the given interval of time.
      Minimum: `1`.
      @default Infinity
      readonly intervalCap?: number;
      The length of time in milliseconds before the interval count resets. Must be finite.
      Minimum: `0`.
      @default 0
      readonly interval?: number;
      Whether the task must finish in the given interval or will be carried over into the next interval count.
      @default false
      readonly carryoverConcurrencyCount?: boolean;
      } & TimeoutOptions;

        type Queue

        type Queue<Element, Options> = {
        size: number;
        filter: (options: Readonly<Partial<Options>>) => Element[];
        dequeue: () => Element | undefined;
        enqueue: (run: Element, options?: Partial<Options>) => void;

          type QueueAddOptions

          type QueueAddOptions = {
          Priority of operation. Operations with greater priority will be scheduled first.
          @default 0
          readonly priority?: number;
          } & TaskOptions &

            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>