An asynchronous iterator library for advanced object pipelines.
npm install asynciteratorAsyncIterator is a lightweight JavaScript implementation of demand-driven object streams,
and an alternative to the two-way flow controlled Node.js Stream.
As opposed to Stream, you cannot _push_ anything into an AsyncIterator;
instead, an iterator _pulls_ things from another iterator.
This eliminates the need for expensive, complex flow control.
Read the full API documentation.
AsyncIterator allows functions toPromise),Iterable):| single value | multiple values | |
|---|---|---|
| synchronous | T getValue() | Iterable<T> getValues() |
| asynchronous | Promise<T> getValue() | AsyncIterator<T> getValues() |
Like Iterable, an AsyncIterator only generates items when you ask it to.
This contrast with patterns such as Observable,
which are data-driven and don't wait for consumers to process items.
EventEmitteriterator.read (yielding null when none is available at the moment)iterator.on('readable', callback)iterator.on('end', callback)iterator.on('data', callback)Any object that conforms to the above conditions can be used with the AsyncIterator library
(this includes Node.js Streams).
The AsyncIterator interface additionally exposes
several other methods and properties.
JavaScript
import https from 'https';
import { resolve } from 'url';
import { IntegerIterator } from 'asynciterator';// Iterate over the natural numbers
const numbers = new IntegerIterator({ start: 0, end: Infinity });
// Transform these numbers into Wikipedia URLs
const urls = numbers.map(n =>
https://en.wikipedia.org/wiki/${n});
// Fetch each corresponding Wikipedia page
const pages = urls.transform((url, done, push) => {
https.get(url, response => {
let page = '';
response.on('data', data => { page += data; });
response.on('end', () => { push(page); done(); });
});
});
// Extract the links from each page
const links = pages.transform((page, done, push) => {
let search = /href="([^"]+)"/g, match;
while (match = search.exec(page))
push(resolve('https://en.wikipedia.org/', match[1]));
done();
});
`We could display a link every 0.1 seconds:
`JavaScript
setInterval(() => {
const link = links.read();
if (link)
console.log(link);
}, 100);
`Or we can get the first 30 links and display them:
`JavaScript
links.take(30).on('data', console.log);
`In both cases, pages from Wikipedia will only be fetched when needed—the data consumer is in control.
This is what makes
AsyncIterator lazy.If we had implemented this using the
Observable pattern,
an entire flow of unnecessary pages would be fetched,
because it is controlled by the data publisher instead.Usage
AsyncIterator implements the EventEmitter interface
and a superset of the Stream interface.$3
By default, an AsyncIterator is in _on-demand_ mode,
meaning it only generates items when asked to.read method returns the next item,
or null when no item is available.`JavaScript
const numbers = new IntegerIterator({ start: 1, end: 2 });
console.log(numbers.read()); // 1
console.log(numbers.read()); // 2
console.log(numbers.read()); // null
`If you receive
null,
you should wait until the next readable event before reading again.
This event is not a guarantee that an item _will_ be available.`JavaScript
links.on('readable', () => {
let link;
while (link = links.read())
console.log(link);
});
`end event is emitted after you have read the last item from the iterator.$3
An AsyncIterator can be switched to _flow_ mode by listening to the data event.
In flow mode, iterators generate items as fast as possible.`JavaScript
const numbers = new IntegerIterator({ start: 1, end: 100 });
numbers.on('data', number => console.log('number', number));
numbers.on('end', () => console.log('all done!'));
`To switch back to on-demand mode, simply remove all
data listeners.$3
An AsyncIterator can have custom properties assigned to it,
which are preserved when the iterator is cloned.
This is useful to pass around metadata about the iterator.`JavaScript
const numbers = new IntegerIterator();
numbers.setProperty('rate', 1234);
console.log(numbers.getProperty('rate')); // 1234const clone = numbers.clone();
console.log(clone.getProperty('rate')); // 1234
numbers.setProperty('rate', 4567);
console.log(clone.getProperty('rate')); // 4567
`You can also attach a callback
that will be called as soon as the property is set:
`JavaScript
const numbers = new IntegerIterator();
numbers.getProperty('later', console.log);
numbers.setProperty('later', 'value');
// 'value'
`$3
Due to the syntactical sugar EcmaScript's AsyncIterator
provides, our iterators can also be consumed as such.
If high performance over large iterators is required, this method of consumption not recommended.`JavaScript
const numbers = new IntegerIterator({ start: 1, end: 100 });for await (const number of numbers)
console.log('number', number);
console.log('all done!');
``Error events emitted within the iterator can be caught by wrapping the for-await-block in a try-catch.
In cases where the returned EcmaScript AsyncIterator will not be fully consumed,
it is recommended to manually listen for error events on the main AsyncIterator
to avoid uncaught error messages.