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.
The channel capacity (0 for rendezvous, >0 for buffered).
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
Indicates whether the channel is closed.
True if the channel is closed, false otherwise.
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.
Wait to receive a message from the channel.
If multiple receivers are waiting, the oldest receiver will be served first (FIFO).
Optionalsignal: AbortSignal | nullAn optional AbortSignal to cancel the receive operation.
A promise that resolves to the received message.
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.
The message to send.
Optionalsignal: AbortSignal | nullA promise that resolves when the message has been sent.
An asynchronous channel for sending and receiving messages between tasks.
Supports both rendezvous (capacity = 0) and buffered (capacity > 0) modes.
Example
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();