@azerum/ts-csp
v0.0.14
Published
Go-style/CSP-style channels for TypeScript with async/await
Readme
Go-style/CSP-style channels for TypeScript with async/await. Features:
async/awaitfriendly API,for await,AbortSignalfor cancellationBuffered & unbuffered channels
select()likeselect{}statement in Go, for reads & writesselect()can also take anyPromiseor any async function that takesAbortSignalTimeout operations with
select()+sleep(); abort viaAbortSignalwithselect()+returnOnAbort()The return type of
select()is inferred for nice exhaustive matchingOperators:
merge(),partitionTime()Works in Node.js and browsers; relies on global
setTimeout,AbortController,AbortSignalZero dependencies
Thoroughly tested
For details see API docs
Install
npm install -E @azerum/ts-cspStability
Experimental: breaking changes to API are expected
Examples
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
Inspired by
- Go channels
- Communicating Sequential Processes - CSP
- @thi.ng/csp - design of return values of
read()andtryRead()
