Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对于循环,然后在每次迭代后,更新;异步/等待";_Javascript_Node.js_Loops_Asynchronous_Async Await - Fatal编程技术网

Javascript 对于循环,然后在每次迭代后,更新;异步/等待";

Javascript 对于循环,然后在每次迭代后,更新;异步/等待";,javascript,node.js,loops,asynchronous,async-await,Javascript,Node.js,Loops,Asynchronous,Async Await,循环之后,如何更新“等待”块的结果 例: for(设i=0;i{ return thing;//thing返回15。 }); for(设j=0;i

循环之后,如何更新“等待”块的结果

例:

for(设i=0;i<5;i++){
让事情等待(foo=>{
return thing;//thing返回15。
});
for(设j=0;i
在内部循环之后,我希望流返回等待块并重新计算值或执行。但这似乎没有发生,因为返回值没有改变

我希望thing第一次返回15次,然后循环15次。在外循环的下一次迭代中,我希望thing返回9,因此内循环返回9次


编辑:对不起,各位,这是正确编写的,问题来自代码的另一部分。谢谢你的帮助

您正在此处定义一个函数:

await ( foo => {
   return thing;     // thing returns 15.
});
但是你从来没有真正调用过这个函数。你可以这样称呼它:

await ( foo => {
   return thing;     // thing returns 15.
})()
…但很难理解为什么创建函数只是为了返回值


另外,在第二个for循环中,定义
j
,然后比较并增加
i
。这很难解释,我认为这可能是一个输入错误。

您只是定义了一个函数,而不是调用它。而且
j
循环看起来可疑

let thing = await ( foo => {
  return thing;     // thing returns 15.
})();
我想这就是你想做的。不过也不完全确定

let thingArr = [15,9]
for (let i = 0; i < 5; i++){

   let thing = await ( foo => {
     return thingArr[i];     // thing returns 15.
   })();

   for (let j = 0; j < thing; j++){
       //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
   }
}
let thingArr=[15,9]
for(设i=0;i<5;i++){
让事情等待(foo=>{
返回thingArr[i];//东西返回15。
})();
for(设j=0;j
当您将值重新分配给内部循环中的
对象
时,您可能正在更新范围
对象
(用
let
声明),该对象将在每次外部迭代后重新创建。我假设您在外部范围中有一个
thing
变量,它在代码开头等于15,如果是这样,您将无法以这种方式更改它。您正在等待一个函数表达式。这毫无意义。这是你真正的密码吗?
let thingArr = [15,9]
for (let i = 0; i < 5; i++){

   let thing = await ( foo => {
     return thingArr[i];     // thing returns 15.
   })();

   for (let j = 0; j < thing; j++){
       //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
   }
}