node.js - 向异步 waterfall 添加异步函数

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

最佳答案

here 得到答案。通过从异步函数返回 args 作为数组,可以在异步 waterfall 中使用异步函数。

像这样

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

关于node.js - 向异步 waterfall 添加异步函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51519330/

相关文章:

javascript - 如何在nuxtServerInit()中获取cookie?

javascript - 当我使用 async/await 时,为什么会收到多个响应?

node.js - NodeJS 连接到 SQL Server getaddrinfo ENOTFOUNT

javascript - 开始使用 Node.js 和 JavaScript

javascript - Nodejs与C++程序通信?

c - `open`和 `close`如何异步串口?

javascript - async/await 链何时停止?

javascript - 面对错误 (0 , (_wordwrap || _load_wordwrap(...)).default)(...)(...).trimStart 不是 expo init 上的函数

javascript - Modernizr - 哪些脚本被异步加载?

node.js - 同步调用Redis Server