How `AsyncIterable` works in IxJS
masterThe AsyncIterable object is based on the ECMAScript Asynchronous Iterators proposal. It allows you to create asynchronous collections of Promises and apply operators like map and filter.
Iteration is performed using the for await ... of statement. You can also use .forEach() and .catch() for handling values and errors in an asynchronous stream.
// ES
import { from } from 'ix/asynciterable';
import { filter, map } from 'ix/asynciterable/operators';
const source = async function* () {
yield 1;
yield 2;
yield 3;
yield 4;
};
const results = from(source()).pipe(
filter(async x => x % 2 === 0),
map(async x => x * x)
);
for await (let item of results) {
console.log(`Next: ${item}`);
}