Created
June 5, 2017 11:55
-
-
Save jeremiahbiard/3efffddb632e05a2de552e6590f5e677 to your computer and use it in GitHub Desktop.
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
/* 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