Skip to main content

Class: AsyncQueue

Defined in: AsyncQueue.ts:6

The AsyncQueue class used to sequentialize burst requests

Constructors​

Constructor​

new AsyncQueue(): AsyncQueue

Returns​

AsyncQueue

Properties​

promises​

private readonly promises: AsyncQueueEntry[] = []

Defined in: AsyncQueue.ts:26

The promises array

Accessors​

queued​

Get Signature​

get queued(): number

Defined in: AsyncQueue.ts:19

The amount of queued entries.

Seealso​

remaining for the count with the head.

Returns​

number


remaining​

Get Signature​

get remaining(): number

Defined in: AsyncQueue.ts:11

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

Seealso​

queued for the queued count.

Returns​

number

Methods​

abortAll()​

abortAll(): void

Defined in: AsyncQueue.ts:83

Aborts all the pending promises.

Returns​

void

Note​

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


shift()​

shift(): void

Defined in: AsyncQueue.ts:65

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

Returns​

void


wait()​

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

Defined in: AsyncQueue.ts:49

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