Skip to content

Instantly share code, notes, and snippets.

@Maccauhuru
Last active March 4, 2019 02:18
Show Gist options
  • Save Maccauhuru/78534b7b7c580e42f46319ed37b7a7b5 to your computer and use it in GitHub Desktop.
Save Maccauhuru/78534b7b7c580e42f46319ed37b7a7b5 to your computer and use it in GitHub Desktop.
Function Generator Example 1
//create a new generator function called takeOff
function* takeOff(){
yield "Three";
yield "Two";
yield "One";
yield "Space Craft taking off....."
}
//assign takeOff to a funtion expression named countDown
const countDown = takeOff();
//iterate the countDown function until you get an object with 'done:true' as a key/value pair
console.log(countDown.next()); //-> {value: "Three", done: false}
console.log(countDown.next()); //-> {value: "Two", done: false}
console.log(countDown.next()); //-> {value: "One", done: false}
console.log(countDown.next()); //-> {value: "Space Craft taking off.....", done: false}
console.log(countDown.next()); //-> {value: undefined, done: true}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment