async-iterator-muxer
v1.2.2
Published
Mux/multiplex together multiple sync and async iterables or iterators into one async iterator.
Downloads
10
Readme
async-iterator-muxer
Mux/multiplex together multiple sync and async iterables or iterators into one async iterator.
Usage
- Create a new instance
- Call
.add([iterable])
to add iterable things - Iterate over the muxer
- Optionally,
.add
more iterables - Optionally, call
.done.get([iterable])
to see it's final result (if applicable)
import AsyncIteratorMuxer from "async-iterator-muxer"
var muxer= new AsyncIteratorMuxer()
// ....
// add some sync & async iterables
muxer.add( myAsyncIterable)
muxer.add( myIterable)
muxer.add( anotherIterable)
// can also add a promise that resolves to something iterable
muxer.add( Promise.resolve([ 4, 5, 6])
// iterate through all outputs as they arrive
for await( var output of muxer){ // for-await must be run inside an async-function, not shown here
console.log( output)
}
muxer.dones.get( myAsyncIterable) // get the return value of myAsyncIterable
Please see ./.example/sample-generator.js for a more complete, runnable, well-explained demonstration: it begins multiple asynchronous generators and multiplexes them together into one async iterable.
There's a variety of hooks and modes available, for coercing values during iteration.