-
-
Save laser/7664292 to your computer and use it in GitHub Desktop.
ES6 Generators
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
function* fibonacci() { | |
var a = 0, b = 1, c = 0; | |
while (true) { | |
yield a; | |
c = a; | |
a = b; | |
b = c + b; | |
} | |
} | |
function run() { | |
var seq = fibonacci(); | |
console.log(seq.next().value); // 0 | |
console.log(seq.next().value); // 1 | |
console.log(seq.next().value); // 1 | |
console.log(seq.next().value); // 2 | |
console.log(seq.next().value); // 3 | |
console.log(seq.next().value); // 5 | |
} | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment