import { Chan } from "https://deno.land/x/easyts@0.1.2/channel.ts";
The concrete realization of Channel
Examples
use
use
function sum(s: Array<number>, c: WriteChannel<number>) {
let sum = 0
for (const v of s) {
sum += v
}
c.write(sum) // send sum to c
}
async function main() {
const s = [7, 2, 8, -9, 4, 0]
const c = new Chan<number>()
sum(s.slice(0, s.length / 2), c)
sum(s.slice(s.length / 2), c)
const [x, y] = [await c.read(), await c.read()] // receive from c
console.log(x, y, x + y)
}
main()
Methods
close(): boolean
Close the channel, after which the channel will not be able to write, all blocked reads and writes are returned, but the value that has been written to the channel is guaranteed to be fully read
read(): ReadReturn<T>
Read a value from the channel, block if there is no value to read, and return until there is a value or the channel is closed
readRaw(): ReadRawReturn<T>
Read a value from the channel, block if there is no value to read, and return until there is a value or the channel is closed
Attempts to read a value from the channel, returns undefined if no value is readable, returns {done:true} if the channel is closed
Static Properties
private
closed_: undefined | Chan<any>private
never_: undefined | Chan<any>readonly
never: ReadChannel<any>Returns a chan that will never have a value, usually used as some token