Async iterable class
AsyncIterable is a class that implement the async iterable
JavaScript pattern,
using a semantic similar to the promise one.
Create an async iterable that emit three numbers:
import AsyncIterable from 'asynciterable';
const numbers = new AsyncIterable((write, end, error) => {
write(1);
write(2);
write(3);
end();
});
for await (const n of numbers) {
console.log(n)
}
// Output: 1\n2\n3\n
A class that implement the async iterable
JavaScript pattern, using a semantic
similar to the promise one.
Parameters
executor
Function A function that is passed with the arguments write, end, error. The executor function is executed immediately by the AsyncIterable implementation, passing write, end and error functions (the executor is called before the AsyncIterable constructor even returns the created object). The write function, when called, make the async iterable emit a new item. The end function, when called, make the async iterable end. The error function make it throw an error. The executor normally initiates some asynchronous work, and then, once a new item is available, it emit it by calling the write function, or else call the error function if an error occurred. If an error is thrown in the executor function, any subsequent call to iterator next is rejected with the same error. The return value of the executor is ignored, unless if it is a promise. In this case, when it fullfills to a rejection, error function is automatically called.
With npm installed, run
npm install --save asynciterable
MIT