channel = new Channel<number>();
const valuesToSend = [1, 2, 3];
const receivedValues: number[] = [];
const receivePromise = async () => {
while (channel.State != ChannelState.EMPTY) {
try {
const value = await channel.receive();
receivedValues.push(value);
} catch {
break; // Stop when the channel is closed
}
}
};
// Send values to the channel
for (const value of valuesToSend) {
channel.send(value);
}
// Start the receive operation in the background
await receivePromise();
channel.close(); // Close the channel when done
// Wait for the background receive operation to finish
await new Promise((resolve) => setTimeout(resolve, 0));
expect(receivedValues).toEqual(valuesToSend);
thanhphuchuynh/ts-channel
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
No description, website, or topics provided.
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published