javascript - 如何在 async.each 内具有 async.waterfall 的外部函数中调用 async.each

标签 javascript node.js asynchronous express mongoose

我在 Node.JS 中有一个导出模块

exports.doSomethingImportant= function(req, res) {
var id = req.params.id;
Demo.findOne({'_id': id})
  .exec(function(err, demosReturned) {
    async.waterfall([
        function(outerCallBack){
          console.log("In the First Call Back");
          firstOrderFunction(demosReturned,outerCallBack);
        },
        function(x,outerCallBack){
           var y =3 
           var z = x*y;
           console.log("In the Second Call Back");
           outerCallBack(null,z);
        }
      ],function(err,z){
        if(err){
          console.log("Error is == " +err);
        }else{
          console.log("The Returned Value is == "+z);
        }
      });
});//End Demo.findOne
};

现在,我的 firstOrderfunction 再次有一个 async.each 嵌入 async.waterfall

function fistOrderFunction(demosReturned,outerCallBack){
console.log("Called the External Function");


async.each(demosReturned.locations, function(location, innerCallBack) {
  console.log('Computing Location #');

    async.waterfall([
          function(internalCallBack){
             console.log("Computing Inner First Waterfall");
               a = 14;
              innternalCallBack(null,a);
          },
          function(a,internalCallBack){
              console.log("Computing Inner Second Waterfall");
               b =14;
               c = a*b;
              innternalBack(null,c)
          }
      ],function(err,c){
        if(err){
          console.log("Error is == " +err);
        }else{
             d = c;
             console.log("The Returned Value is == "+c);
             innerCallBack(null,d);
        }
    });//End Async.Waterfall
},function(err,d){
    if(err){enter code here
      console.log("The Error in Async.Each === " + err);
    }else{
      console.log("The Returned Value is Processed ");
      outerCallBack(null, d);
    }
}); //End Async.Each
}

我得到的输出是

<小时/>

In the First Call Back

Called the External Function

Computing Location #

Computing Location #

Computing Inner First Waterfall

Computing Inner First Waterfall

The Returned Value is Processed

In the Second Call Back

The Returned Value is == NaN

我希望所有内容都按以下顺序同步运行。

  1. Call async.waterfall in exec call back of Demo.findone

  2. Call the firstOrderFunction

  3. Call async.each inside firstOrderFunction

  4. Call async.waterfall inside async.each

  5. Call the first callback function returning a=14.

  6. Call the second callback function returning c =14*14 =196.

如何使用异步实现此目的?

提前致谢,并对这么长的问题表示歉意。

最佳答案

在 async.waterfall() 的末尾调用 async.each() 的回调,并在 async.each() 的末尾调用 firstOrderFunction 的回调。这是修改后的代码:

function fistOrderFunction(demosReturned, callback){
  var ret = [];

  console.log("Called the External Function");
  async.each(demosReturned.locations, function(location, eachCb) {
    console.log('Computing Location #');

    async.waterfall([
        function(waterfallCb){
            console.log("Computing Inner First Waterfall");
            a = 14;
            waterfallCb(null,a);
        },
        function(a,waterfallCb){
            console.log("Computing Inner Second Waterfall");
            b =14;
            c = a*b;
            waterfallCb(null,c)
        }
    ],function(err,c){
        if(err){
            console.log("Error is == " +err);
            eachCb(err);
        }else{
            ret.push(c);
            console.log("The Returned Value is == "+c);
            eachCb(null);
        }
    });//End Async.Waterfall
  },function(err){
    if(err){
        console.log("The Error in Async.Each === " + err);
        callback(err, null);
    }else{
        console.log("The Returned Value is Processed ");
        callback(null, ret);
    }
  }); //End Async.Each
}

关于javascript - 如何在 async.each 内具有 async.waterfall 的外部函数中调用 async.each,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29382803/

相关文章:

javascript - 我应该在 JS 中哪里定义 emscripten extern 函数?

javascript - 将所有路由重定向到 Heroku 托管的 nuxt 项目中的 https

javascript - Rails - 时间轴 javascript 的路由问题

node.js - Elasticsearch查询返回具有给定ID的文档

javascript - 如何在处理 HTTP 请求之前将 csv 文件同步加载到内存中

node.js - npm - 不想在 package.json 内的 devDependency 上列出包

node.js - Express-Validator 中的验证

javascript - 如何从 html 中查找数据并使用 jQuery 存储它

.net - dotNet 中是否有 DirectoryInfo.GetFiles/Directory.GetDirectories 的异步版本?

javascript - 火狐浏览器 : No click events while JavaScript is running