Skip to content

Channel

Defined in: packages/async/src/synchro/channel.ts:65

An asynchronous channel for sending and receiving messages between tasks.

Supports both rendezvous (capacity = 0) and buffered (capacity > 0) modes.

const ch = new Channel(0); // Rendezvous channel

// Sender task
async function sender() {
await ch.send(42);
console.log("Message sent");
}
// Receiver task
async function receiver() {
const msg = await ch.receive();
console.log("Received message:", msg);
}
// Start sender and receiver
sender();
receiver();
```
@example
const ch = new Channel<string>(10); // Buffered channel with capacity 10
// Sender task
async function sender() {
for (let i = 0; i < 20; i++) {
await ch.send(`Message ${i}`);
console.log(`Sent: Message ${i}`);
}
}
// Receiver task
async function receiver() {
for (let i = 0; i < 20; i++) {
const msg = await ch.receive();
console.log(`Received: ${msg}`);
}
}
// Start sender and receiver
sender();
receiver();
@template T The type of messages carried. Anything except `undefined`, which
the channel reserves for "no message".

T extends DefinedValue

new Channel<T>(capacity?): Channel<T>

Defined in: packages/async/src/synchro/channel.ts:108

Create a new Channel with the given capacity.

A capacity of 0 creates a rendezvous channel where sends and receives must be paired. A capacity greater than 0 creates a buffered channel.

number = Infinity

The channel capacity (0 for rendezvous, >0 for buffered).

Channel<T>

const rendezvousChannel = new Channel<number>(0);
const bufferedChannel = new Channel<string>(5);
const ch = new Channel<number>(3); // Buffered channel with capacity 3
await ch.send(1);
await ch.send(2);
await ch.send(3);
// The next send will wait until a receive occurs
const sendPromise = ch.send(4);
const msg = await ch.receive(); // Receives 1
await sendPromise; // Now the send of 4 completes
const ch = new Channel<number>(0); // Rendezvous channel
const sendPromise = ch.send(42); // This will wait for a receiver
const msg = await ch.receive(); // Receives 42, unblocking the sender
await sendPromise; // Now the send completes

If capacity is negative.

readonly capacity: number = Infinity

Defined in: packages/async/src/synchro/channel.ts:108

The channel capacity (0 for rendezvous, >0 for buffered).

get closed(): boolean

Defined in: packages/async/src/synchro/channel.ts:134

Indicates whether the channel is closed.

boolean

True if the channel is closed, false otherwise.

close(): void

Defined in: packages/async/src/synchro/channel.ts:240

Close the channel. All pending and future receives will reject.

If the channel is already closed, this is a no-op.

Any pending producers/receivers will be rejected with ChannelClosedError.

void


receive(signal?): Promise<T>

Defined in: packages/async/src/synchro/channel.ts:198

Wait to receive a message from the channel.

If multiple receivers are waiting, the oldest receiver will be served first (FIFO).

AbortSignal | null

An optional AbortSignal to cancel the receive operation.

Promise<T>

A promise that resolves to the received message.

If the channel is closed.


send(message, signal?): Promise<void>

Defined in: packages/async/src/synchro/channel.ts:152

Send a message through the channel.

If the channel capacity is zero (“rendezvous” channel), this will wait for a receiver to be ready.

If the capacity is greater than zero (buffered), this will buffer the message or wait if the buffer is full. If a receiver is waiting, the message is delivered directly to the receiver.

T

The message to send.

AbortSignal | null

Promise<void>

A promise that resolves when the message has been sent.

If the channel is closed.