Class: AsyncQueue<T>
AsyncQueue
is a class that represents an asynchronous queue.
It allows values to be pushed onto it and consumed (pulled) by an iterator.
The queue is async-iterable, making it compatible with async/await syntax.
Example
const queue = new AsyncQueue<number>();
queue.push(1);
(async () => {
for await (const value of queue) {
console.log(value);
}
})();
Type parameters
Name | Description |
---|---|
T | The type of elements contained in the queue. |
Implements
AsyncIterable
<T
>
Constructors
constructor
• new AsyncQueue<T
>(): AsyncQueue
<T
>
Type parameters
Name |
---|
T |
Returns
AsyncQueue
<T
>
Methods
[asyncIterator]
▸ [asyncIterator](): AsyncIterator
<T
, any
, undefined
>
Creates and returns an async iterator that allows the queue to be consumed. You can create multiple iterators for the same queue.
Returns
AsyncIterator
<T
, any
, undefined
>
An async iterator for the queue.
Example
(async () => {
for await (const value of queue) {
console.log(value);
}
})();
Implementation of
AsyncIterable.[asyncIterator]
Defined in
packages/modelfusion/src/util/AsyncQueue.ts:78
close
▸ close(): void
Closes the queue, preventing more elements from being pushed onto it.
Returns
void
Example
queue.close();
Defined in
packages/modelfusion/src/util/AsyncQueue.ts:61
error
▸ error(error
): void
Parameters
Name | Type |
---|---|
error | unknown |
Returns
void
Defined in
packages/modelfusion/src/util/AsyncQueue.ts:46
push
▸ push(value
): void
Pushes an element onto the queue. If the queue is closed, an error is thrown.
Parameters
Name | Type | Description |
---|---|---|
value | T | The element to add to the queue. |
Returns
void
Throws
Throws an error if the queue is closed.
Example
queue.push(2);