import { type Receiver } from "https://deno.land/x/async_channels@1.0.0-alpha45/mod.ts";
Methods
Receive returns a promise that will be resolved with [T, true]
when a value is available, or rejected if a
provided AbortController
is aborted
If the channel is closed, then the promise will be resolved immediately with [undefined, false]
.
Receiving from a closed channel:
import {Channel} from "./channel.ts";
const ch = new Channel();
ch.close();
const [val, ok] = await ch.receive()
console.assert(val === undefined)
console.assert(ok === false)
Receiving from a buffered channel:
import {Channel} from "./channel.ts";
const ch = new Channel(1);
await ch.send("Hello world!")
ch.close();
const [val, ok] = await ch.receive()
console.assert(val === "Hello world!")
console.assert(ok === true)
Aborting a receive request:
import {Channel, AbortedError} from "./channel.ts";
const ch = new Channel(1);
await ch.send("Hello world!")
ch.close();
const abortCtrl = new AbortController()
abortCtrl.abort()
try {
await ch.receive(abortCtrl);
console.assert(false, "unreachable");
} catch (e) {
console.assert(e instanceof AbortedError);
}
Creates an AsyncGenerator
that yields all values sent to this channel,
and returns when the channel closes.
map returns a receiver channel that contains the results of applying fn
to each value of this
channel.
The receiver channel will close, when the original channel closes.
forEach applies fn
to each value in this
channel, and returns a channel
that will close after this
channel closes.