@harnyk/chan
v0.1.1
Published
This package is my humble attempt to provide a `Chan<T>` class for TypeScript, which would be as much as possible similar to Go's `chan T` type.
Downloads
4,138
Readme
@harnyk/chan
- Go channels for TypeScript
This package is my humble attempt to provide a Chan<T>
class for TypeScript, which would be as much as possible similar to Go's chan T
type.
Installation
npm install @harnyk/chan
Usage
See the tests and examples for more information.
Brief reference:
ch := make(chan int)
const ch = new Chan<number>();
ch := make(chan int, 5)
const ch = new Chan<number>(5);
for v := range ch {
fmt.Println(v)
}
for await (const v of ch) {
console.log(v);
}
ch <- 42
await ch.send(42);
v, ok := <-ch
const [v, ok] = await ch.recv();
close(ch)
ch.close();
select {
case ch <- 42:
fmt.Println("sent")
case v := <-ch1:
fmt.Printf("Received %d\n", v)
default:
fmt.Println("default")
}
await select()
.send(ch, 42, () => console.log('sent'))
.recv(ch1, (v) => console.log(`Received ${v}`))
.default(() => console.log('default'));
What is supported
- asynchronous iterating over
Chan<T>
withfor await
- asynchronous
send
andrecv
select
-ing over multiple channels
License
WTFPL
Contributors
Mark Harnyk (https://github.com/harnyk)