Skip to content

Instantly share code, notes, and snippets.

@ltciro
Last active October 29, 2018 09:03
Show Gist options
  • Save ltciro/8314442473d8aeae79d9971b5ca7373f to your computer and use it in GitHub Desktop.
Save ltciro/8314442473d8aeae79d9971b5ca7373f to your computer and use it in GitHub Desktop.
//importar la librería IxJS
const Ix = require('ix');
//Crea la fuente de datos con una función async generator
async function* arrGen() {
yield 1;
yield 2;
yield 4;
}
//Crea un objeto que IxJS entienda; Async Iterable.
const results = Ix.AsyncIterable.from(arrGen())
.filter(x => x % 2 === 0) // filtra y devuelve sólo los datos que son pares
.map(x => x * 2); // y el resultado Anterior lo multiplica por 2
const resultFromIterator = results[Symbol.asyncIterator]() // obtenemos la instancia del iterator
//como son objetos asíncronos utilizamos async await para obtener la respuesta
async function arrReadFromIterator() {
console.log( 'next 4', await resultFromIterator.next() )
console.log( 'next 8',await resultFromIterator.next() )
console.log( 'next?', await resultFromIterator.next() )
}
//llamado de la función asíncrona
arrReadFromIterator()
// next 4
// {value: 4, done: false}
// next 8
// {value: 8, done: false}
// next?
// {value: undefined, done: true}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment