Skip to content

Instantly share code, notes, and snippets.

@ltciro
Last active November 13, 2018 06:07
Show Gist options
  • Save ltciro/c5acf9c36860a63ef876088b34edab41 to your computer and use it in GitHub Desktop.
Save ltciro/c5acf9c36860a63ef876088b34edab41 to your computer and use it in GitHub Desktop.
//se crea la función generadora con la anotación "function*"
const arr = function* arrGen(){
yield 1 // yield detiene la función, devuelve 1 y cuándo se ejecute de nuevo next()
// continuá en la siguiente linea de código la #5
yield 2 // yield detiene la función, devuelve 2 y cuándo se ejecute de nuevo next()
// continuá en la siguiente linea de código la #7
yield 4 // yield detiene la función, devuelve 2 y cuándo se ejecute de nuevo next()
// devulve undefined y la propiedad done igual a true
}
const arrInstance = arr();
console.log('Next in array_as_generator 1', arrInstance.next())
// Next in array_as_generator 1
// {value: 1, done: false}
console.log('Next in array_as_generator 2', arrInstance.next())
//Next in array_as_generator 2
//{value: 2, done: false}
console.log('Next in array_as_generator 4', arrInstance.next())
//Next in array_as_generator 4
//{value: 4, done: false}
console.log('Next in array_as_generator?', arrInstance.next())
//Next in array_as_generator?
//{value: undefined, done: true}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment