Skip to content

Instantly share code, notes, and snippets.

@mugyu
Last active April 27, 2019 02:32
Show Gist options
  • Save mugyu/dbdc991bba84bffcd2801ce59ca8111d to your computer and use it in GitHub Desktop.
Save mugyu/dbdc991bba84bffcd2801ce59ca8111d to your computer and use it in GitHub Desktop.
Generator in JavaScript
// 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