【发布时间】:2018-09-01 16:38:44
【问题描述】:
循环后,如何更新“await”块的结果?
前:
for (let i = 0; i < 5; i++){
let thing = await ( foo => {
return thing; // thing returns 15.
});
for (let j = 0; i < thing; i++){
//will loop 15 times the first time (out of 5 times because of outer loop)
//want to change value of thing to 9, so the next time it loops 9 times
}
}
在内循环之后,我期待流程返回到等待块并重新评估值或执行。但这似乎没有发生,因为返回值是'没有改变。
我希望 thing 第一次返回 15,然后循环 15 次。在外循环的下一次迭代中,我希望 thing 返回 9,因此内循环 9 次。
编辑:对不起大家这是正确的,问题来自代码的另一部分。感谢您的帮助!
【问题讨论】:
-
当您在内部循环中将值重新分配给
thing时,您可能正在更新您的作用域thing(用let声明),它将在每次外部迭代后重新创建。我假设您在外部范围中有一个thing变量,在代码开头等于 15,如果是这样,您将无法以这种方式更改它。 -
你是
await一个函数表达式。这是没有意义的。这是你的真实代码吗?
标签: javascript node.js loops asynchronous async-await