Skip to content

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.

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 consumed

Replay 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 2
for await (const v of channel) console.log("B", v); // B 1, B 2

Broadcast mode - simultaneous consumers

const channel = AsyncChannel.broadcast<number>();
// Start two consumers
const 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 1
channel.push(2); // Both consumers receive 2
channel.close();
await Promise.all([consumer1, consumer2]);
// 1: 1, 2: 1, 1: 2, 2: 2

ReplayBroadcast mode - late subscribers catch up

const channel = AsyncChannel.broadcastReplay<number>();
channel.push(1);
channel.push(2);
// Consumer A starts - gets 1, 2 from replay
const 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 waits
const consumerB = (async () => {
for await (const v of channel) console.log("B:", v);
})();
channel.push(4); // Both A and B get 4
channel.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); // 2
Type Parameter Default type
T -
TReturn void
  • AsyncIterable<T>
get closed(): boolean;

Defined in: async-channel.ts:312

Returns true if the channel is closed.

boolean


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).

number

[asyncIterator](): AsyncIterator<T, TReturn>;

Defined in: async-channel.ts:350

AsyncIterator<T, TReturn>

AsyncIterable.[asyncIterator]


close(returnValue?): void;

Defined in: async-channel.ts:249

Close the channel with an optional return value.

Parameter Type
returnValue? TReturn

void


push(value): void;

Defined in: async-channel.ts:205

Push a value to be yielded by the iterator(s).

Parameter Type
value T

void

If the channel is already closed


reject(error): void;

Defined in: async-channel.ts:284

Reject the channel with an error.

Parameter Type
error unknown

void


reset(): void;

Defined in: async-channel.ts:329

Reset the channel to its initial state.

void


resolve(returnValue): void;

Defined in: async-channel.ts:277

Resolve the channel with a return value. Alias for channel.close(returnValue).

Parameter Type
returnValue TReturn

void


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 Parameter Default type
TItem -
TRet void

AsyncChannel<TItem, TRet>


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 Parameter Default type
TItem -
TRet void

AsyncChannel<TItem, TRet>


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 Parameter Default type
TItem -
TRet void

AsyncChannel<TItem, TRet>


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 Parameter Default type
TItem -
TRet void

AsyncChannel<TItem, TRet>