node js异步回调问题

时间:2016-09-30 06:39:24

标签: node.js waterfall

我有一点async瀑布的问题,因为我想在循环处理的函数之后调用下一个函数。脚本是异步瀑布流的一部分,之前的函数工作正常。这里唯一的事情是它有一个for循环。代码如下。任何帮助将非常感谢。谢谢

function(querySize,req)
{
console.log("in second function call :" +querySize);
var temp,title;
for(var counter = 0;counter <= querySize;counter++)
{
    websiteAddressArray[counter]  = req.query.address[counter];
    console.log("for  is : " +req.query.address[counter]);
    console.log("first line is : " +websiteAddressArray[counter]);
    console.log("second line  is : " +websiteAddressArray[counter]);
    request(websiteAddressArray[counter], function(err, resp, body)
    {
        $ = cheerio.load(body);
        console.log("title is : " +$('title').text());
        titleArray[counter] =  $('title').text();   
    });
  }
  var line="this is string";
  callback(null,line);
 },
 function(callback,line)
 {
  console.log("line is : " +line)
 },

1 个答案:

答案 0 :(得分:1)

您的syntax不正确。始终传递async.waterfall

中的callback最后一个参数
 function(querySize, req,callback) {
  console.log("in second function call :" + querySize);
  var temp, title;
  for (var counter = 0; counter <= querySize; counter++) {
    websiteAddressArray[counter] = req.query.address[counter];
    console.log("for  is : " + req.query.address[counter]);
    console.log("first line is : " + websiteAddressArray[counter]);
    console.log("second line  is : " + websiteAddressArray[counter]);
    request(websiteAddressArray[counter], function(err, resp, body) {
      $ = cheerio.load(body);
      console.log("title is : " + $('title').text());
      titleArray[counter] = $('title').text();
    });
  }
  var line = "this is string";
  callback(null, line);
},
function(callback, line) {
  console.log("line is : " + line)
},