Skip to content

Instantly share code, notes, and snippets.

@tanner-west
Created February 8, 2022 12:48
Show Gist options
  • Save tanner-west/358b5c9330c751c9c089188490a08b9b to your computer and use it in GitHub Desktop.
Save tanner-west/358b5c9330c751c9c089188490a08b9b to your computer and use it in GitHub Desktop.
function* myGeneratorFunction(){
console.log("Hello generator!")
yield "Yield result one"
console.log("Hello generator again!")
yield "Yield result two"
return "Generator is done"
}
const gen = myGeneratorFunction()
console.log("Calling next the first time")
const resultOne = gen.next();
console.log(resultOne);
console.log("Calling next the second time")
const resultTwo = gen.next();
console.log(resultTwo);
console.log("Calling next the third time")
const resultThree = gen.next();
console.log(resultThree);
console.log("Calling next the fourth time")
const resultFour = gen.next();
console.log(resultFour);
// Output:
// Calling next the first time
// Hello generator!
// { value: 'Yield result one', done: false }
// Calling next the second time
// Hello generator again!
// { value: 'Yield result two', done: false }
// Calling next the third time
// { value: 'Generator is done', done: true }
// Calling next the fourth time
// { value: undefined, done: true }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment