【问题标题】:Node async.js multiple function with waterfall带瀑布的 Node async.js 多功能
【发布时间】:2015-10-16 23:44:29
【问题描述】:

我不是 node 和 async.js 方面的专家。所以期待 node js 社区的帮助。所以这是我的场景,我想一个接一个地做 3 次操作,每个都取决于以前的结果。通过使用以前的结果,我需要调用异步函数来检索另一个结果。所以我决定使用异步瀑布。 (希望我选择了正确的一个)。

async.waterfall([
  function(callback) {
    request({
            method: 'GET',
            headers: {'Content-Type' : 'application/json'},
            url    : url
        },function(error, response, body){
              if(error) {
                  callback(error);
              } else {
                   var result= JSON.parse(body);
                   callback(null,result); //sending to next function
              }
        });
  },
  function(result, callback) {
    //here i want to use the result array in loop async.eachSeries or forEachSeries and fetch another result array using request module then send it to next function
    /*Here I don't know How to process the result*/
    callback(null,result1)
  },
  function(result1, callback) {
    //here i want to use the result1 array and fetch another result array then send it to next function
    callback(null,result2)
  }
], function(error, res) {
  console.log(res); // process final result 
});

我参考了一些教程。我无法理解这就是为什么最终会在这里。提前致谢。

【问题讨论】:

  • 我想帮你但我真的不明白你的问题是什么
  • 嗨,我第一步成功了,(将结果发送到第二个函数)。我真的不知道如何在第二步中使用循环和回调进行处理
  • 我现在正在回复,请尝试编辑您自己的问题,更明确地说明您的问题

标签: javascript node.js async.js


【解决方案1】:

只是必须在瀑布函数系列中的每个函数上使用传入的每个 cb。

顺便说一句,我不得不说:

  1. 不要嵌套异步函数
  2. 尝试改用 Promise
  3. 总是检查整个异步模块功能,很多时候,有些东西可以并行运行

好的,所以对于waterfall函数,基本上需要以下语法,语义上:

async.waterfall(arrayOfFunctions, [optionalResolveCallbackFunction]);

函数数组的第一个元素的语法如下:

function (cb) {...};

以下 n 个函数需要以下语法:

function (param1, param2, ... paramN, cb){...}

因此,最后一个参数将是用于转到下一个函数或返回错误的参数。 瀑布参数的数量是可选的。

每个cb 函数都遵循错误回调 约定,其中error 是要传递的第一个参数。如果数组中的任何函数返回错误,则执行将被中断,代码将转到最后一个optionalResolveCallbackFunction

因此,当您决定终止循环 async.eachSeries 或 forEachSeries 时,即使这样也会产生复杂的代码(甚至容易出现性能问题的风险),您将使用 callback 对象,它恰好是瀑布函数的回调参数,用于传递给后续函数或结束执行。

【讨论】:

    【解决方案2】:

    这是一个如何工作的例子:

    async.waterfall([
        function(callback) {
            request({
                method: 'GET',
                headers: {
                    'Content-Type': 'application/json'
                },
                url: url
            }, function(error, response, body) {
                if (error) {
                    callback(error);
                } else {
                    var result = JSON.parse(body);
                    callback(null, result); //sending to next function
                }
            });
        },
        function(result, callback) {
            //here i want to use the result array in loop async.eachSeries or forEachSeries and fetch another result array using request module then send it to next function
            async.each(result, function(item, callback) {
                request({
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    url: url
                }, function(error, response, body) {
                    if (error) {
                        callback(error);
                    } else {
                        var result = JSON.parse(body);
                        callback(null, result);
                    }
                });
            }, function(err, result1) {
                if (!err) {
                    callback(null, result1);
                } else {
                    callback(err);
                }
            });
        },
        function(result1, callback) {
            request({
                method: 'GET',
                headers: {
                    'Content-Type': 'application/json'
                },
                url: url
            }, function(error, response, body) {
                if (error) {
                    callback(error);
                } else {
                    var result2 = JSON.parse(body);
                    callback(null, result2);
                }
            });
        }
    ], function(error, res) {
        console.log(res); // process final result 
    });
    

    【讨论】:

    • 我认为这是我想要的解决方案。我会检查这个解决方案。谢谢你的时间。非常感谢。
    • 嗨,我试过打印结果1,得到undefinedconsole.log(result1)`
    • 使用asyncmapSeries 代替async.each。成功了
    【解决方案3】:

    当函数调用失败时,你应该返回以确保你没有进入下一步:

    function(callback) {
        request({
                method: 'GET',
                headers: {'Content-Type' : 'application/json'},
                url    : url
            },function(error, response, body){
                  if(error) {
                      return callback(error);
                  } else {
                       var result= JSON.parse(body);
                       callback(null,result); //sending to next function
                  }
            });
      },
    

    第二个函数中的结果参数是第一次调用返回的数组。

    function(result, callback) {
        //here i want to use the result array in loop async.eachSeries or forEachSeries and fetch another result array using request module then send it to next function
        /*Here I don't know How to process the result*/
        for (var i in result) {
        ...
        }
        callback(null,result1)
      },
    

    对于系列中的下一个函数也是如此

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      • 2018-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-02
      相关资源
      最近更新 更多