import { Chan } from "https://deno.land/x/easyts@0.1.3/mod.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()
buffered
buffered
const ch = new Chan<number>(2)
ch.write(1)
ch.write(2)
let [v,ok]= ch.readRaw()
console.log(v,ok)
v = ch.read()
console.log(v)
Methods
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 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
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
Wait for chan to close, no data will be read from chan
Writes a value to the channel, blocking if the channel is not writable until the channel is writable or closed
Implement asynchronous iterators
Static Properties
Returns a alreay closed chan, usually used as some token
Returns a chan that will never have a value, usually used as some token