I get confused about the syntax for generator in node environment.
I run the script as: node --harmony simpleGenerator.js, but still have problem. My node environment is Linux (v0.11.5 / v0.11.13)
Syntax Error: Unexpected identifier
/home/NodeProj/ES6-generator/simpleGenerator.js:6
yield x;
^
script:
function foo(x) {
while (true) {
x = x * 2;
yield x;
}
}
var gen = foo(1);
console.log(gen.next().value);
What's going on here ?
A generator needs an asterisk after the function keyword:
function* foo(x) {
while (true) {
x = x * 2;
yield x;
}
}