Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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 如何使此异步代码在此for循环内运行_Javascript_Loops_For Loop_Synchronization - Fatal编程技术网

Javascript 如何使此异步代码在此for循环内运行

Javascript 如何使此异步代码在此for循环内运行,javascript,loops,for-loop,synchronization,Javascript,Loops,For Loop,Synchronization,我有一个for…of循环,里面有一个。“then”是我最后的选择,我尝试过承诺和简单的承诺,但我就是无法让它发挥作用。(为了澄清:它需要按顺序在all1数组中循环 let all1 = await VCLB.findAll({order: [['time', 'DESC']], limit: 10}); let list1 = []; let q = 0; for(item of all1) { if(!message.guild.members.get(item.id)) re

我有一个for…of循环,里面有一个。“then”是我最后的选择,我尝试过承诺和简单的承诺,但我就是无法让它发挥作用。(为了澄清:它需要按顺序在all1数组中循环

let all1 = await VCLB.findAll({order: [['time', 'DESC']], limit: 10});
  let list1 = [];
  let q = 0;
for(item of all1) {
    if(!message.guild.members.get(item.id)) return;
    if(item.time == 0) return;
          let time = item.time
    VC.findOne({where: {id: item.id}}).then(findVC => {
      if(!findVC) return;
        let timeNow = new Date().getTime();
        let startVC = findVC.time;
        let difference = timeNow - startVC;
        time = time + difference;

    })
    q++;
    time = prettyMilliseconds(time)
    list1.push(`**${q}.** | ${message.guild.members.get(item.id)} | **${time}** `)

  }
不起作用的部分是

    VC.findOne({where: {id: item.id}}).then(findVC => {
      if(!findVC) return;
        let timeNow = new Date().getTime();
        let startVC = findVC.time;
        let difference = timeNow - startVC;
        time = time + difference;

    })
即使它在那里找到了什么,它也不会更新时间变量。我知道这是不可能的。然后()可以工作,但老实说,我没有其他线索知道如何在没有某种类型的异步的情况下做到这一点。这段代码必须同步,因为在收集数据后,它将所有数据放入一个单数字符串中。在我尝试使用wait来等待数据库搜索之前,但显然在这种情况下,这是不可能的,因为我需要等待数据首先被删除已收集。任何允许在继续之前搜索数据库的解决方案都可以

非常感谢
Sam

这类问题很容易用setTimeout作为一个最小的例子来重现,然后你就可以去掉所有的积垢。通常,创建最小生产量可以帮助你解决这个问题。使你的函数
异步
,只需按顺序调用,而不必担心所有的
。然后()
让findVC=等待VC.findOne(…);
等等?