Last active
April 27, 2019 02:32
-
-
Save mugyu/dbdc991bba84bffcd2801ce59ca8111d to your computer and use it in GitHub Desktop.
Generator in JavaScript
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
// JavaScript Generator | |
function* getGenerator(index) { | |
while(index < 3) | |
{ | |
yield index++; | |
} | |
} | |
const generator = getGenerator(0); | |
console.log(generator); | |
console.log(generator.next().value); | |
console.log(generator.next().value); | |
console.log(generator.next().value); | |
console.log(generator.next().value); | |
//=> Object [Generator] {} | |
//=> 0 | |
//=> 1 | |
//=> 2 | |
//=> undefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment