你能从封闭中屈服吗?
// 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
答案 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}}