0

I'm writing steam bot that calculates price of sent items. I can't use functions properly. I want to get price from URL then to add it up and console.log.

I can't do it, because console.log executes before the loop.

I'm really new to Javascript and I can't fix it :(

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+'$.' );

Function getting the price for 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);
    });

}
irqize
  • 97
  • 1
  • 10
  • 1
    `whole_price variable can't be accessed inside callback` Why is that? – XCS Jan 17 '16 at 20:58
  • Wow, i modified the code, and it can(probably before i did typo), but still console.log executes before the loop. – irqize Jan 17 '16 at 21:03
  • 1
    That is because the `getPrice` function is async. You have to count the number of callbacks that have completed and only log the value when `count == offer.itemsToReceive.length`. Or you can use the `async` library or some promise library and wait for all callbacks. https://github.com/caolan/async – XCS Jan 17 '16 at 21:05

2 Answers2

3

Best way to do something like this is...

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

this is basically making sure you don't log the message until all requests have been made and responded to. This is because even though all the calls are made in a specific order they may complete in a different order.

jacob bullock
  • 651
  • 4
  • 14
2

You should consider using an async library, and make your life easier. Don't reinvent the wheel!

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);
});
Dmitry Matveev
  • 5,320
  • 1
  • 32
  • 43