Skip to content

Instantly share code, notes, and snippets.

@kazu69
Created March 12, 2014 04:28
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 kazu69/9500860 to your computer and use it in GitHub Desktop.
Save kazu69/9500860 to your computer and use it in GitHub Desktop.
ES6 generators function
function* double(x) {
while(true) {
x = x * 2;
yield x;
}
}
var g = double(2);
console.log(g.next()); // # => 4
console.log(g.next()); // # => 8
console.log(g.next()); // # => 16
function* count() {
yield 1;
yield 2;
yield 3;
yield 4;
return 5;
}
var c = count();
console.log(c.next()); // # => 1
console.log(c.next()); // # => 2
console.log(c.next()); // # => 3
console.log(c.next()); // # => 4
console.log(c.next()); // # => 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment