使用瀑布节点async.js多function

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

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

我提到了一些教程。 我不明白这就是为什么在这里结束。 提前致谢。

只能使用瀑布函数系列中每个函数传入的每个cb。

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

  1. 不要嵌套asynchronousfunction
  2. 尝试使用承诺,而不是
  3. 总是检查整个asynchronous模块function,很多时候,一些东西可以并行运行

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

 async.waterfall(arrayOfFunctions, [optionalResolveCallbackFunction]); 

函数数组中的第一个元素具有以下语法:

 function (cb) {...}; 

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

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

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

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

因此,当你决定你的循环终止async.eachSeries或forEachSeries时,即使这样会产生复杂的代码(或者甚至是容易出现性能问题的风险),你将使用恰好是callback参数的callback对象瀑布函数要么传递给下面的函数,要么终止执行。

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

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

当函数调用失败时,您应该返回以确保您不会进入下一步:

 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) }, 

然后在系列中的下一个function相同