向异步瀑布添加异步功能

时间:2018-07-25 12:50:02

标签: 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 {
                    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);
}

1 个答案:

答案 0 :(得分:0)

here得到答案。通过从异步函数返回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) {});
相关问题