Skip to main content
Module

x/p_queue/mod.ts>default

Promise queue with concurrency control, for Deno
Latest
class default
extends EventTarget
import { default } from "https://deno.land/x/p_queue@1.0.1/mod.ts";

Promise queue with concurrency control.

Constructors

new
default(options?: Options<QueueType, EnqueueOptionsType>)

Returns a new queue instance, which is an EventTarget (https://developer.mozilla.org/en-US/docs/Web/API/EventTarget) subclass.

Type Parameters

optional
QueueType extends Queue<RunFunction, EnqueueOptionsType> = PriorityQueue
optional
EnqueueOptionsType extends QueueAddOptions = DefaultAddOptions

Properties

private
readonly
_carryoverConcurrencyCount: boolean
private
_concurrency: number
private
readonly
_doesConcurrentAllowAnother: boolean
private
readonly
_doesIntervalAllowAnother: boolean
private
readonly
_interval: number
private
readonly
_intervalCap: number
private
_intervalCount: number
private
_intervalEnd: number
private
optional
_intervalId: number
private
readonly
_isIntervalIgnored: boolean
private
_isPaused: boolean
private
_pendingCount: number
private
_queue: QueueType
private
readonly
_queueClass: new () => QueueType
private
_resolveEmpty: ResolveFunction
private
_resolveIdle: ResolveFunction
private
readonly
_throwOnTimeout: boolean
private
optional
_timeout: number
private
optional
_timeoutId: number
concurrency: number
readonly
isPaused: boolean

Whether the queue is currently paused.

readonly
pending: number

Number of running items (no longer in the queue).

readonly
size: number

Size of the queue, the number of queued items waiting to run.

timeout: number | undefined

Set the timeout for future operations.

Methods

private
_isIntervalPaused(): boolean
private
_next(): void
private
_onInterval(): void
private
_onResumeInterval(): void
private
_processQueue(): void

Executes all queued functions until it reaches the limit.

private
_resolvePromises(): void
private
_tryToStartAnother(): boolean
add<TaskResultType>(fn: Task<TaskResultType>, options?: Partial<EnqueueOptionsType>): Promise<TaskResultType>

Adds a sync or async task to the queue. Always returns a promise.

addAll<TaskResultsType>(functions: ReadonlyArray<Task<TaskResultsType>>, options?: EnqueueOptionsType): Promise<TaskResultsType[]>

Same as .add(), but accepts an array of sync or async functions.

clear(): void

Clear the queue.

onEmpty(): Promise<void>

Can be called multiple times. Useful if you for example add additional items at a later time.

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.

pause(): void

Put queue execution on hold.

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.

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.)