Skip to content

Instantly share code, notes, and snippets.

@laser
Created November 26, 2013 19:15
Show Gist options
  • Save laser/7664292 to your computer and use it in GitHub Desktop.
Save laser/7664292 to your computer and use it in GitHub Desktop.
ES6 Generators
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