Skip to content

Instantly share code, notes, and snippets.

@anibal21
Created April 20, 2022 19:39
Show Gist options
  • Save anibal21/62050f3d74b51479bed118cbf0ce3263 to your computer and use it in GitHub Desktop.
Save anibal21/62050f3d74b51479bed118cbf0ce3263 to your computer and use it in GitHub Desktop.
Example of generator functions in JS.
const handsomeArray = [
"Option1",
"Option2",
"Option3",
"Option4",
"Option5",
"Option6"
]
function * generatorFunction(array) { // Line 1
for (item in array){
yield array[item]
}
}
const generatorObject = generatorFunction(handsomeArray);
for (let item; item = generatorObject.next().value; )
console.log(item)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment