Skip to main content

Class: AsyncQueue

@sapphire/async-queue.AsyncQueue

The AsyncQueue class used to sequentialize burst requests

Constructors

constructor

new AsyncQueue(): AsyncQueue

Returns

AsyncQueue

Properties

promises

Private promises: AsyncQueueEntry[] = []

The promises array

Defined in

lib/AsyncQueue.ts:26

Accessors

queued

get queued(): number

The amount of queued entries.

Returns

number

Seealso

remaining for the count with the head.

Defined in

lib/AsyncQueue.ts:19


remaining

get remaining(): number

The amount of entries in the queue, including the head.

Returns

number

Seealso

queued for the queued count.

Defined in

lib/AsyncQueue.ts:11

Methods

abortAll

abortAll(): void

Aborts all the pending promises.

Returns

void

Note

To avoid race conditions, this does not unlock the head lock.

Defined in

lib/AsyncQueue.ts:83


shift

shift(): void

Unlocks the head lock and transfers the next lock (if any) to the head.

Returns

void

Defined in

lib/AsyncQueue.ts:65


wait

wait(options?): Promise<void>

Waits for last promise and queues a new one

Parameters

NameType
options?Readonly<AsyncQueueWaitOptions>

Returns

Promise<void>

Example

const queue = new AsyncQueue();
async function request(url, options) {
await queue.wait({ signal: options.signal });
try {
const result = await fetch(url, options);
// Do some operations with 'result'
} finally {
// Remove first entry from the queue and resolve for the next entry
queue.shift();
}
}

request(someUrl1, someOptions1); // Will call fetch() immediately
request(someUrl2, someOptions2); // Will call fetch() after the first finished
request(someUrl3, someOptions3); // Will call fetch() after the second finished

Defined in

lib/AsyncQueue.ts:49