@facetlayer/open-async-iterator
v1.0.0
Published
A utility for creating async iterators that are driven externally
Readme
open-async-iterator
A small utility for creating async iterators that can be controlled externally.
This allows you to take events from a callback-based context (such as Node.js streams), and pipe those events into an async iterator, where it can be used in any function that uses async operations.
Installation
npm install @facetlayer/open-async-iteratorUsage
import { openAsyncIterator } from '@facetlayer/open-async-iterator'
// Create the iterator.
const { send, done, iterator } = openAsyncIterator<number>()
// The 'driver' code sends values to the iterator and marks it as done.
send(1);
send(2);
send(3);
done();
// Then a different part of the code consumes the iterator (this can happen in parallel)
for await (const value of iterator) {
console.log(value) // Outputs: 1, 2, 3
}API
openAsyncIterator<T>()
Creates a new open async iterator.
Returns:
send(value: T)- Function to send a value to the iteratordone()- Function to mark the iterator as completeiterator- The async iterator object
Error Handling
Calling send() after done() will throw an error:
const { send, done } = openAsyncIterator()
done()
send(1) // Throws: "usage error: called send() after done()"