Skip to main content

Class: AsyncQueue

The AsyncQueue class used to sequentialize burst requests

Constructors

new AsyncQueue()

new AsyncQueue(): AsyncQueue

Returns

AsyncQueue

Properties

promises

private promises: AsyncQueueEntry[] = []

The promises array

Source

lib/AsyncQueue.ts:26

Accessors

queued

get queued(): number

The amount of queued entries.

Seealso

remaining for the count with the head.

Returns

number

Source

lib/AsyncQueue.ts:19


remaining

get remaining(): number

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

Seealso

queued for the queued count.

Returns

number

Source

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.

Source

lib/AsyncQueue.ts:83


shift()

shift(): void

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

Returns

void

Source

lib/AsyncQueue.ts:65


wait()

wait(options?: Readonly <AsyncQueueWaitOptions>): Promise<void>

Waits for last promise and queues a new one

Parameters

ParameterType
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

Source

lib/AsyncQueue.ts:49