0.1.1 • Published 1 year ago
@harnyk/chan v0.1.1
@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 <- 42await ch.send(42);v, ok := <-chconst [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
sendandrecv select-ing over multiple channels
License
WTFPL
Contributors
Mark Harnyk (https://github.com/harnyk)