Skip to main content

Class: AsyncQueue

@sapphire/async-queue.AsyncQueue

The AsyncQueue class used to sequentialize burst requests

Constructors

constructor

โ€ข new AsyncQueue()

Properties

promises

โ€ข Private promises: InternalAsyncQueueDeferredPromise[] = []

The promises array

Defined in

projects/utilities/packages/async-queue/src/lib/AsyncQueue.ts:15

Accessors

remaining

โ€ข get remaining(): number

The remaining amount of queued promises

Returns

number

Defined in

projects/utilities/packages/async-queue/src/lib/AsyncQueue.ts:8

Methods

shift

โ–ธ shift(): void

Frees the queue's lock for the next item to process

Returns

void

Defined in

projects/utilities/packages/async-queue/src/lib/AsyncQueue.ts:56


wait

โ–ธ wait(): Promise<void>

Waits for last promise and queues a new one

example

const queue = new AsyncQueue();
async function request(url, options) {
await queue.wait();
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

Returns

Promise<void>

Defined in

projects/utilities/packages/async-queue/src/lib/AsyncQueue.ts:38