Skip to content

Instantly share code, notes, and snippets.

@mightyguava
Created March 12, 2017 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mightyguava/fe779d4a31bd0ddbbeb2d4bbb7b37c4c to your computer and use it in GitHub Desktop.
Save mightyguava/fe779d4a31bd0ddbbeb2d4bbb7b37c4c to your computer and use it in GitHub Desktop.
Demystifying Async Programming in Javascript - Generator counts()
function* counts(start) {
yield start + 1;
yield start + 2;
yield start + 3;
return start + 4;
}
const counter = counts(0);
console.log(counter.next()); // {value: 1, done: false}
console.log(counter.next()); // {value: 2, done: false}
console.log(counter.next()); // {value: 3, done: false}
console.log(counter.next()); // {value: 4, done: true}
console.log(counter.next()); // {value: undefined, done: true}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment