Last active
October 29, 2018 09:03
-
-
Save ltciro/8314442473d8aeae79d9971b5ca7373f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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