Javascript js等待异步函数完成,然后继续执行其余代码

Javascript js等待异步函数完成,然后继续执行其余代码,javascript,node.js,mongodb,asynchronous,mongoose,Javascript,Node.js,Mongodb,Asynchronous,Mongoose,在我的代码中,有两个for循环执行一个异步函数(两个循环中的函数都是相同的),但在这两个循环之后,代码必须等到它们执行后才能运行。这是我的代码: for(var a = 0; a < videoIds.length; a++){ if(videoIds){ findVideo(videoIds[a], function(thumbnailPath, videoName){ // findVideo is the async function // it return

在我的代码中,有两个for循环执行一个异步函数(两个循环中的函数都是相同的),但在这两个循环之后,代码必须等到它们执行后才能运行。这是我的代码:

for(var a = 0; a < videoIds.length; a++){
  if(videoIds){
    findVideo(videoIds[a], function(thumbnailPath, videoName){ // findVideo is the async function
      // it returns 2 values, thumbnailPath and videoName
      videoNames[a] = videoName; // and then these 2 values are written in the arrays
      thumbnaildPaths[a] = thumbnailPath;
      console.log('1');
    });
  }
}

// then the above code runs one more time but with different values, so I won't include it here (it's the same thing)
enter code here
console.log('3');
// the rest of the code
// writes the values from the loops to the database so I can't run it many times 
for(var a=0;a

如果我运行代码,我会在看到1之前看到3(从console.log函数)。但正如我上面所说的,我必须等待循环结束,然后才能继续。
findVideo()
函数只包含mongoose提供的Video.find({})方法,然后返回值thumbnailPath和videoName。我需要做的是等待这两个循环结束,然后继续,但由于明显的原因,我不能将其余的代码放在循环中!有办法解决这个问题吗?谢谢大家!

您可以使用回调,但我更喜欢承诺,因为它们很优雅

利用
Promise.all()


最简单的解决方案是只使用回调。只需将循环封装在函数中并执行类似操作

function findVid(callback) {
    for(var a = 0; a < videoIds.length; a++){
        if(videoIds){
            findVideo(videoIds[a], function(thumbnailPath, videoName){ // findVideo is the async function
                // it returns 2 values, thumbnailPath and videoName
                videoNames[a] = videoName; // and then these 2 values are written in the arrays
                thumbnaildPaths[a] = thumbnailPath;
                console.log('1');
                if (callback) callback(); //This will be called once it has returned
            });
        }
    }
}
findVid(function(){ 
    //this will be run after findVid is finished.
    console.log('3');
    // Rest of your code here.
});
函数findVid(回调){
对于(var a=0;a

您也可以使用承诺样式而不是回调,但是这两种样式都适用于工作。为了进一步了解回调和承诺,我找到了一篇很好的文章,将更详细地解释一切:

函数的循环在那里,只是为了确保某些选定的视频确实存在,并且它们由用户上传(确切地说是req.user)您可以查看
async
npm模块来管理异步控制流。他们在那里就是为了这个目的。好的,但是在这个循环之后,我有2个
for
循环,所以我必须把
if(callback)callback()放进去第二个函数中的函数?
function findVid(callback) {
    for(var a = 0; a < videoIds.length; a++){
        if(videoIds){
            findVideo(videoIds[a], function(thumbnailPath, videoName){ // findVideo is the async function
                // it returns 2 values, thumbnailPath and videoName
                videoNames[a] = videoName; // and then these 2 values are written in the arrays
                thumbnaildPaths[a] = thumbnailPath;
                console.log('1');
                if (callback) callback(); //This will be called once it has returned
            });
        }
    }
}
findVid(function(){ 
    //this will be run after findVid is finished.
    console.log('3');
    // Rest of your code here.
});