Skip to content

Instantly share code, notes, and snippets.

@jeremiahbiard
Created June 5, 2017 11:55
Show Gist options
  • Save jeremiahbiard/3efffddb632e05a2de552e6590f5e677 to your computer and use it in GitHub Desktop.
Save jeremiahbiard/3efffddb632e05a2de552e6590f5e677 to your computer and use it in GitHub Desktop.
/* Simple example of generator function in javascript */
let timer;
function* generatorFn() {
let iterations = 0;
let done = false
while(!done) {
console.log(`I've run ${iterations} times!`);
iterations += 1;
if (iterations === 6) {
done = true;
}
yield;
}
clearInterval(timer);
console.log('All done!');
}
const generator = generatorFn();
timer = setInterval(() => {
generator.next();
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment