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.
Example
Section titled “Example”const ch = new Channel
// Sender taskasync function sender() {await ch.send(42);console.log("Message sent");}
// Receiver taskasync function receiver() {const msg = await ch.receive();console.log("Received message:", msg);}// Start sender and receiversender();receiver();```
@exampleconst ch = new Channel<string>(10); // Buffered channel with capacity 10
// Sender taskasync function sender() {for (let i = 0; i < 20; i++) {await ch.send(`Message ${i}`);console.log(`Sent: Message ${i}`);}}
// Receiver taskasync function receiver() {for (let i = 0; i < 20; i++) {const msg = await ch.receive();console.log(`Received: ${msg}`);}}
// Start sender and receiversender();receiver();
@template T The type of messages carried. Anything except `undefined`, whichthe channel reserves for "no message".Type Parameters
Section titled “Type Parameters”T extends DefinedValue
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”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.
Parameters
Section titled “Parameters”capacity?
Section titled “capacity?”number = Infinity
The channel capacity (0 for rendezvous, >0 for buffered).
Returns
Section titled “Returns”Channel<T>
Examples
Section titled “Examples”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 completesconst 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 completesThrows
Section titled “Throws”If capacity is negative.
Properties
Section titled “Properties”capacity
Section titled “capacity”
readonlycapacity:number=Infinity
Defined in: packages/async/src/synchro/channel.ts:108
The channel capacity (0 for rendezvous, >0 for buffered).
Accessors
Section titled “Accessors”closed
Section titled “closed”Get Signature
Section titled “Get Signature”get closed():
boolean
Defined in: packages/async/src/synchro/channel.ts:134
Indicates whether the channel is closed.
Returns
Section titled “Returns”boolean
True if the channel is closed, false otherwise.
Methods
Section titled “Methods”close()
Section titled “close()”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.
Returns
Section titled “Returns”void
receive()
Section titled “receive()”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).
Parameters
Section titled “Parameters”signal?
Section titled “signal?”AbortSignal | null
An optional AbortSignal to cancel the receive operation.
Returns
Section titled “Returns”Promise<T>
A promise that resolves to the received message.
Throws
Section titled “Throws”If the channel is closed.
send()
Section titled “send()”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.
Parameters
Section titled “Parameters”message
Section titled “message”T
The message to send.
signal?
Section titled “signal?”AbortSignal | null
Returns
Section titled “Returns”Promise<void>
A promise that resolves when the message has been sent.
Throws
Section titled “Throws”If the channel is closed.