2015-10-06 110 views
0


我试图执行多个迭代函数,并且无法获取node.js异步类型的工作。如何在异步函数中等待请求方法

async.waterfall([ 
    function (callback) { 
     // function gets amount of pages 
    }, 
    function (amountOfPages, callback) { 
     //addes a url to each page 
    }, 
    function (links, callback) { 
     //Now i need to go over each link , and extract info from that page 
     var tradelinks = []; 
     async.each(links, function (link, callback){ 

      console.log("In loop : "+ link) // Until this part its doing all right , then it hangs on the request 

      request(link , function (error, response, body) { 
       //extract from page... 
      }) 
      callback() 
     }) 
     callback(null, tradelinks); 
    } 
], function (err, result) { 
    console.log("Done") 
    console.log(result) 
}); 

基本上它在正确的顺序前两个函数,那么它走遍了联系,并不会等待请求函数执行,所以即时得到结果中的一个空白阵列和done标志,只有之后,它会打印从页面中提取的数据。

我的输出看起来像这样

Function 1 done 
Function 2 done 
In the async.each loop // for how many links there are 
Done 
[] //only after this point the data extracted from page appears 
extracted data // for how many links there are 

我的新用这种编程,和真的不知道如何从着手这儿过得知道这可与承诺或那样的事情要做,但是我似乎无法得到它的工作。

+0

async.each()丢失它的完整的回调(这是瀑布迭代回调应该叫) –

+1

尝试使用'cb',而不是'callback'在'each'循环。 'async'会认为瀑布步骤已经完成.. – Rayon

+0

尝试过使用cb,没有工作。你是什么意思,因为错过了完整的回调? –

回答

1

您的问题似乎是您在异步流程之外发布callback(null, tradelinks)。你需要让你的.each的回调,它会通知每个迭代完成:

async.each(links, function() { stuff here }, function() { 
    callback(null, tradelinks); 
}); 
+0

Are you sure?不会打破'each'并处理下一个瀑布步骤? – Rayon

+0

你能解释为什么它需要它吗? –

+0

因为在处理任何异步代码之前,代码将继续整个同步路径。如果回调在异步库之外被调用,它就是同步代码的一部分,所以它会在任何异步函数响应之前被调用。 –

-1

在异步调用的工作与在堆栈中的回调函数。看看这个:

function init(){ 
    /* 1- stack */ 
    async("any data", function(data){ 
     first(data); 
    }); 
} 

function async(data, callback){ 
    /* 
    . 
    . Code async 
    . 
    */ 
    callback(data); 
} 

function first(data){ 
    /* 2- stack */ 
    async("any data", function(data){ 
     second(data); 
    }); 
} 

function second(data, callback){ 
    /* 3- stack */ 
    async("any data", function(data){ 
     third(data); 
    }); 
} 

function third(data){ 
    console.log(data); 
} 
+0

这将如何解决问题? – Rayon

+0

工作多重异步功能时,需要回拨功能。在我的例子中显示这一点 –

0

问题是请求也是异步的。这里是你在做什么:

async.each(links, function (link, callback){ 

     console.log("In loop : "+ link) 

     request(link , function (error, response, body) { 
      //I am async too. And I just fire off and async.each 
      //processes the next item in the array, fires me off again, 
      //and never waits for me to complete. You really need to 
      //put async.each's callback inside me and all will be well. 
      //Also, change async.each's callback to cb because while 
      //Javascript's functional scope makes it ok, it's still 
      //confusing to read. 
     }) 
     callback() 
    }) 

所以,新的代码应该这样做,你会没事的。

async.each(links, function (link, cb){ 

     console.log("In loop : "+ link) 

     request(link , function (error, response, body) { 
      //do stuff synchronously 
      cb() 
     }) 

    })