[英]How to use Array.prototype.reduce in generator with yield inside?
I would like to run a reduce function in which I want pause it via yield.我想运行一个减少 function 我想通过产量暂停它。 Here is what I tried and failed because error:
Uncaught SyntaxError: Unexpected identifier
这是我尝试但失败的方法,因为错误:
Uncaught SyntaxError: Unexpected identifier
function* abc() {
return [1,2,3].reduce((accumulator, currentValue) => {
accumulator.push(currentValue);
yield currentValue;
return accumulator;
}, []);
}
You can't yield
inside a callback - any yield
s must be directly inside a generator function.您不能在回调中
yield
- 任何yield
都必须直接在生成器 function 中。 You'll have to transform the reduce
into something else.您必须将
reduce
转换为其他内容。
function* abc() { const accumulator = []; for (const currentValue of [1, 2, 3]) { yield currentValue; accumulator.push(currentValue); } // do something with accumulator? } console.log(...abc());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.