CSP-style channels for TypeScript with async
/await
. Inspired by:
core.async
read()
and tryRead()
that avoid extra allocationsnpm install @azerum/ts-csp
Write familiar procedural code with async
/await
to process streams of data,
while respecting backpressure. No more callback hell
Buffered & unbuffered channels
select()
function similar to select{}
statement in Go, alts!
in Clojure
(currently, only reads are supported)
Cancellation of reads using AbortSignal
, see select()
Some useful operators: merge()
, partitionTime()
, timeout()
Focus on TS ergonomics: strict types, inferred nicely when possible
(e.g. see return type of select()
)
Works in Node.js and browsers; relies on global setTimeout
, AbortController
No dependencies
Thoroughly tested
API docs describe what each method
on Channel
does and more
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 results back
Batch processing: use of partitionTime()
:
process channel in groups of N items. Useful e.g. to save data in DB in batches