Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.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
Node.js 向异步瀑布添加异步函数_Node.js_Asynchronous_Async Await - Fatal编程技术网

Node.js 向异步瀑布添加异步函数

Node.js 向异步瀑布添加异步函数,node.js,asynchronous,async-await,Node.js,Asynchronous,Async Await,我正在尝试开发一个NodeJS应用程序,但它无法按预期执行。我想先执行downloadIMG,然后执行featureMatching,并继续执行,直到for循环终止。指导我以正确的方式重写代码 for (var i=0; i<dbImgCount; i++) { (function(i) { async.waterfall([ async function downloadIMG(done) { try {

我正在尝试开发一个NodeJS应用程序,但它无法按预期执行。我想先执行downloadIMG,然后执行featureMatching,并继续执行,直到for循环终止。指导我以正确的方式重写代码

for (var i=0; i<dbImgCount; i++) {
    (function(i) {
        async.waterfall([
            async function downloadIMG(done) {
                try {
                    var options = {
                        url:  FolderPath[i].FolderPath,
                        dest: '/home/ubuntu/imgCompare/'                
                    }
                    const { filename, image } =  await download.image(options);
                    image2 = 'image.jpg';
                    done(null, 'hi');
                } catch (e) {
                    console.error(e)
                }
            },
            async function featureMatching(a, done){
                const img1 = cv.imread(image1);
                const img = 'image.jpg';
                const img2 = cv.imread(img);
                const orbMatchesImg = matchFeatures({
                    img1,
                    img2,
                    detector: new cv.ORBDetector(),
                    matchFunc: cv.matchBruteForceHamming
                    },
                    (console.log(image1+','+img))
                );
                done(null);
            }
        ],
        function (err) {});
    })(i);
}
for(var i=0;i从中得到了答案。异步函数可以通过从异步函数返回args作为数组来在异步瀑布中使用

像这样

  async.waterfall([
           // ...
         async function (arg1, arg2) {
          //...
             const arg3 = await foo()

             return [arg1, arg2, arg3] //USE THIS, OTHERWISE 2ND fn WON'T WORK
         },
         function ([arg1, arg2, arg3], callback) {
           //...
         }
  ],function (err) {});

还有一种更简单、更优雅的方法。异步函数是承诺。Async.js处理回调,而不是承诺。使用Async-q,它是为承诺而设计的:避免只链接的Anwer,因为它们随时都可能消失。相反,在答案中添加链接源的相关部分。@k0pernikus嘿,我提到了答案的相关部分在我的评论中!尝试使用新的async/Wait…如果你在NodeJ上投入时间,最好学习它们,它们会让事情变得更简单。不再需要使用async之类的库。