【发布时间】:2019-11-19 06:53:40
【问题描述】:
我想运行一个 reduce 函数,我想通过 yield 暂停它。
这是我尝试并失败的原因,因为错误:
Uncaught SyntaxError: Unexpected identifier
function* abc() {
return [1,2,3].reduce((accumulator, currentValue) => {
accumulator.push(currentValue);
yield currentValue;
return accumulator;
}, []);
}
【问题讨论】:
-
你不能。
yield不跨越函数边界。你到底想做什么? -
@FelixKling 我想暂停并恢复该功能,同时检查当前状态以确定是否恢复。
-
您可以构建自己的
reduce版本,它可以做到这一点。
标签: javascript generator