ES6 / ES2015函数关闭内的收益率*

时间:2016-01-23 20:45:41

标签: javascript ecmascript-6 yield

你能从封闭中屈服吗?

// I want the following to work but instead I get:
//   Uncaught SyntaxError: missing ) after argument list(…)

function* test() {
    yield 1;
    [2,3].map(x => yield x);
    yield 4;
}

var gen = test();
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
console.log(gen.next().value); // 3
console.log(gen.next().value); // 4

1 个答案:

答案 0 :(得分:1)

您应该使用yield将控件传递给另一个可迭代对象 https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/yield*

您还尝试在非生成器函数中return进行function* test() { yield 1; yield* [2,3].map((x) => {return x}); yield 4; } var gen = test(); console.log(gen.next().value); // 1 console.log(gen.next().value); // 2 console.log(gen.next().value); // 3 console.log(gen.next().value); // 4 。只需使用{{1}}

即可
{{1}}