Skip to content

Mux/multiplex together multiple sync and async iterables or iterators into one async iterator.

Notifications You must be signed in to change notification settings

rektide/async-iterator-muxer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

async-iterator-muxer

Mux/multiplex together multiple sync and async iterables or iterators into one async iterator.

Usage

  1. Create a new instance
  2. Call .add([iterable]) to add iterable things
  3. Iterate over the muxer
  4. Optionally, .add more iterables
  5. 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.

About

Mux/multiplex together multiple sync and async iterables or iterators into one async iterator.

Resources

Stars

Watchers

Forks

Packages

No packages published