从网页刮板返回数据,asynchronous问题

我陷入了一个JavaScriptcallback问题。 我已经使用了一个节点库来从客户端网站抓取数据,然后成功https://github.com/rchipka/node-osmosis 。 现在我需要它在另一个循环内进行刮擦和返回。 我会愚蠢的代码,但它的东西是这样的。

// within a loop of another parsing library (simplified) .on("data", function(obj){ listing = { listing['img'] = scrape(obj['ID']); } priceArray.push(listing); }) .on("end", function(){ // use this data }) // osmosis scrape function to be used inside loop // just need it to return value so it can be pushed into array above function scrape(id){ osmosis .get('http://www.xxxxxx.com/searchnew.aspx?sv=~'+ id) .find('.productImg > a') .set('img', '@img') .data(function(listing) { newImg = listing; }) .done(function() { return newImg; }); } 

现在我知道这是callback函数,并且在done参数中返回不是正确的方法。 但是如果我在scrape函数中设置了一个variables,并且在底部返回它的空白bc,那么在osmosis命令运行之前就会处理它。 只是在循环内杀死我堆叠callback。 请帮忙