AsyncChannel
Defined in: async-channel.ts:145
A push-based AsyncIterable that allows external control over yielded values.
Similar to Completer but for iterables: producers call push(value) to
yield an item, close(returnValue?) (or its alias resolve) to end the
iteration, or reject(error) to make the consumer’s for await throw.
Consumers read it with for await. When a consumer is waiting, push hands
the value straight to it; otherwise the value is buffered until one asks.
push after close/reject throws, until reset().
T is the item type; TReturn (default void) is the iterator’s return
value, set by close(returnValue).
- consume (via
create()) - Values are consumed by first iterator, subsequent iterations get nothing - replay (via
replay()) - All values are buffered, each iterator reads from the start (sequential) - broadcast (via
broadcast()) - Multiple simultaneous consumers, each gets values pushed after subscribing (nothing is kept for late subscribers) - broadcastReplay (via
broadcastReplay()) - Multiple simultaneous consumers, new subscribers get past values + future values
There is no public constructor; pick the mode with a static factory.
Examples
Section titled “Examples”Basic usage (consume mode)
import { AsyncChannel } from "@xtandard/lib";
const channel = AsyncChannel.create<number>();
channel.push(1);channel.push(2);channel.close();
for await (const value of channel) { console.log(value); // 1, 2}// Second iteration gets nothing - values already consumedReplay mode - multiple iterations
const channel = AsyncChannel.replay<number>();
channel.push(1);channel.push(2);channel.close();
for await (const v of channel) console.log("A", v); // A 1, A 2for await (const v of channel) console.log("B", v); // B 1, B 2Broadcast mode - simultaneous consumers
const channel = AsyncChannel.broadcast<number>();
// Start two consumersconst consumer1 = (async () => { for await (const v of channel) console.log("1:", v);})();const consumer2 = (async () => { for await (const v of channel) console.log("2:", v);})();
channel.push(1); // Both consumers receive 1channel.push(2); // Both consumers receive 2channel.close();
await Promise.all([consumer1, consumer2]);// 1: 1, 2: 1, 1: 2, 2: 2ReplayBroadcast mode - late subscribers catch up
const channel = AsyncChannel.broadcastReplay<number>();
channel.push(1);channel.push(2);
// Consumer A starts - gets 1, 2 from replayconst consumerA = (async () => { for await (const v of channel) console.log("A:", v);})();
channel.push(3); // A gets 3
// Consumer B starts late - gets 1, 2, 3 from replay, then waitsconst consumerB = (async () => { for await (const v of channel) console.log("B:", v);})();
channel.push(4); // Both A and B get 4channel.close();
await Promise.all([consumerA, consumerB]);// A: 1, B: 1, A: 2, B: 2, A: 3, B: 3, A: 4, B: 4// (each consumer sees 1, 2, 3, 4 in order)Error handling
const channel = AsyncChannel.create<string>();
channel.push("before error");channel.reject(new Error("Something went wrong"));
try { for await (const value of channel) { console.log(value); // "before error" }} catch (error) { console.error(error); // Error: Something went wrong}Reset and reuse
const channel = AsyncChannel.replay<number>();
channel.push(1);channel.close();for await (const v of channel) console.log(v); // 1
channel.reset();channel.push(2);channel.close();for await (const v of channel) console.log(v); // 2Type Parameters
Section titled “Type Parameters”| Type Parameter | Default type |
|---|---|
T |
- |
TReturn |
void |
Implements
Section titled “Implements”AsyncIterable<T>
Accessors
Section titled “Accessors”closed
Section titled “closed”Get Signature
Section titled “Get Signature”get closed(): boolean;Defined in: async-channel.ts:312
Returns true if the channel is closed.
Returns
Section titled “Returns”boolean
pending
Section titled “pending”Get Signature
Section titled “Get Signature”get pending(): number;Defined in: async-channel.ts:322
Returns the number of values in the channel’s shared buffer: the unread
values in consume mode, every value pushed so far in replay and
broadcastReplay mode (late iterators replay them), and always 0 in
broadcast mode (values go straight to the current subscribers).
Returns
Section titled “Returns”number
Methods
Section titled “Methods”[asyncIterator]()
Section titled “[asyncIterator]()”[asyncIterator](): AsyncIterator<T, TReturn>;Defined in: async-channel.ts:350
Returns
Section titled “Returns”AsyncIterator<T, TReturn>
Implementation of
Section titled “Implementation of”AsyncIterable.[asyncIterator]
close()
Section titled “close()”close(returnValue?): void;Defined in: async-channel.ts:249
Close the channel with an optional return value.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
returnValue? |
TReturn |
Returns
Section titled “Returns”void
push()
Section titled “push()”push(value): void;Defined in: async-channel.ts:205
Push a value to be yielded by the iterator(s).
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
value |
T |
Returns
Section titled “Returns”void
Throws
Section titled “Throws”If the channel is already closed
reject()
Section titled “reject()”reject(error): void;Defined in: async-channel.ts:284
Reject the channel with an error.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
error |
unknown |
Returns
Section titled “Returns”void
reset()
Section titled “reset()”reset(): void;Defined in: async-channel.ts:329
Reset the channel to its initial state.
Returns
Section titled “Returns”void
resolve()
Section titled “resolve()”resolve(returnValue): void;Defined in: async-channel.ts:277
Resolve the channel with a return value.
Alias for channel.close(returnValue).
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
returnValue |
TReturn |
Returns
Section titled “Returns”void
broadcast()
Section titled “broadcast()”static broadcast<TItem, TRet>(): AsyncChannel<TItem, TRet>;Defined in: async-channel.ts:188
Create a channel in broadcast mode. Multiple simultaneous consumers each receive values pushed after subscribing.
Type Parameters
Section titled “Type Parameters”| Type Parameter | Default type |
|---|---|
TItem |
- |
TRet |
void |
Returns
Section titled “Returns”AsyncChannel<TItem, TRet>
broadcastReplay()
Section titled “broadcastReplay()”static broadcastReplay<TItem, TRet>(): AsyncChannel<TItem, TRet>;Defined in: async-channel.ts:196
Create a channel in broadcastReplay mode. Multiple simultaneous consumers, new subscribers get all past values + future values.
Type Parameters
Section titled “Type Parameters”| Type Parameter | Default type |
|---|---|
TItem |
- |
TRet |
void |
Returns
Section titled “Returns”AsyncChannel<TItem, TRet>
create()
Section titled “create()”static create<TItem, TRet>(): AsyncChannel<TItem, TRet>;Defined in: async-channel.ts:172
Create a channel in consume mode. Values are consumed by the first iterator, subsequent iterations get nothing.
Type Parameters
Section titled “Type Parameters”| Type Parameter | Default type |
|---|---|
TItem |
- |
TRet |
void |
Returns
Section titled “Returns”AsyncChannel<TItem, TRet>
replay()
Section titled “replay()”static replay<TItem, TRet>(): AsyncChannel<TItem, TRet>;Defined in: async-channel.ts:180
Create a channel in replay mode. All values are buffered and each iterator reads from the start.
Type Parameters
Section titled “Type Parameters”| Type Parameter | Default type |
|---|---|
TItem |
- |
TRet |
void |
Returns
Section titled “Returns”AsyncChannel<TItem, TRet>