Go-style/CSP-style channels for TypeScript with async/await. Features:
-
async/awaitfriendly API,for await,AbortSignalfor cancellation -
Buffered & unbuffered channels
-
select()likeselect{}statement in Go, for reads & writes -
select()can also take anyPromiseor any async function that takesAbortSignal -
Timeout operations with
select()+sleep(); abort viaAbortSignalwithselect()+returnOnAbort() -
The return type of
select()is inferred for nice exhaustive matching -
Operators:
merge(),partitionTime() -
Works in Node.js and browsers; relies on global
setTimeout,AbortController,AbortSignal -
Zero dependencies
-
Thoroughly tested
For details see API docs
npm install -E @azerum/ts-cspExperimental: breaking changes to API are expected
See src/_examples directory:
-
Ping-pong: common introductory example of channels in Go
-
Fast producer and slow consumer: demonstrates how backpressure works
-
Fan-out, fan-in: a common pattern to distribute work among N workers and merge the results back
-
select(): -
Batch processing: use of
partitionTime(): process channel in groups of N items. Useful e.g. to save data in DB in batches
- Go channels
- Communicating Sequential Processes - CSP
- @thi.ng/csp - design of return values of
read()andtryRead()