2016-01-17 85 views
0

我正在写蒸汽机器人来计算已发送物品的价格。我无法正确使用功能。我想从URL获得价格,然后将其添加到console.log中。异步JavaScript和功能

我不能这样做,因为console.log在循环之前执行。

我真的很新的JavaScript,我不能修复:(

var whole_price = 0; 
for(var i=0 ; i<offer.itemsToReceive.length; i++){ 
    getPrice(offer.itemsToReceive[i].market_hash_name, function(price){ 
     whole_price += price; 
    }); 
} 
console.log('Accepted offer from ' + offer.partner + ' with ' + offer.itemsToReceive.length + ' items valued as '+whole_price+'$.'); 

功能得到了URL价格:

function getPrice(name, callback){ 
    name = name.replace(/\ +/g, '%20'); 
    var url = 'http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name='+name; 
    var price = 0; 


    request(url ,function(error, res, body){ 
     var useCSGOBACKPACK = false; 
     if(!error && res.statusCode == 200){ 
      body = JSON.parse(body); 
      if(body.success == true){ 
       price = body.median_price.substr(1); 
      }else{ 
       useCSGOBACKPACK = true; 
      } 
     }else{ 
      useCSGOBACKPACK = true; 
     } 

     if(useCSGOBACKPACK==true){ 
      url = 'http://csgobackpack.net/api/GetItemPrice/?id='+name+'&currency=USD'; 
      request(url, function(error, res, body){ 
       body = JSON.parse(body); 
       price = body.median_price; 
      }); 
     } 

     callback(price); 
    }); 

} 
+1

'whole_price变量不能在回调里访问'为什么? – Cristy

+0

哇,我修改了代码,它可以(可能之前,我没有错字),但仍然console.log执行循环之前。 – irqize

+1

这是因为'getPrice'函数是异步的。您必须计算已完成的回调数,并且只在count = offer.itemsToReceive.length时记录该值。或者您可以使用'async'库或某个承诺库并等待所有回调。 https://github.com/caolan/async – Cristy

回答

3

做这样的事情的最佳方法是...

var whole_price = 0; 
var requestsMade = 0; 
for(var i=0 ; i<offer.itemsToReceive.length; i++){ 
    requestsMade++; 
    getPrice(offer.itemsToReceive[i].market_hash_name, function(price){ 
     whole_price += price; 
     requestsMade++; 

     if(requestsMade == offer.itemsToReceive.length-1) 
     { 
      console.log(YOUR MESSAGE); 
     } 
    }); 
} 

这基本上确保你不记录我直到所有请求都已完成并作出响应。这是因为即使所有呼叫都是按特定顺序进行的,它们也可以按不同的顺序完成。

+0

谢谢你们,你救了我的底部:D – irqize

2

你应该考虑使用async library,让你的生活更轻松。不要重新发明轮子!

var whole_price = 0; 

async.each(offer.itemsToReceive, function (item, next) { 

    getPrice(item.market_hash_name, (price) => { 
     whole_price += price; 
     next(); 
    }); 

}, function() { 

    console.log(whole_price); 
});