0

This is my first week in node so I'm sorry if this is a no brainier.

The code works and does what it should. But I can't figure out how to match the name (url) that starts http.get whit the result it gets from the website.

I found this witch is almost like my problem, except this is a premade function so I can't edit the function and add a callback.

variable scope in asynchronous function

If I could run this code synchronous or make a callback in the http.get function it would all be good. But I don't have the skills and don't know if you even can do it.

Thanks - Robin.

http = require('http');
function download(name) {
//name is an array whit csgo items names.
for (var i = 0; i < name.length; i++) {

    var marketHashName = getGoodName(name[i]);
    var url = 'http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=' + marketHashName;

    http.get(url, function (res) {
        var data = "";
        res.on('data', function (chunk) {
            data += chunk;
        });
        res.on("end", function () {

            data = JSON.parse(data);
            var value= 0;
            //get the value in the json array
            if(data.median_price) {
                value = data.median_price;
            }else{
                value = data.lowest_price;
            }

            value = value.substr(5);
            console.log("WEAPON",value);
            //callback whit name/link and value?
            //callback(name,value);
        });
    }).on("error", function () {

    });
}

}

1 Answer 1

1

You can just add a callback argument and then call it with the final data. And, if you want to pass to the callback the particular marketHashName that was being processed, then you can create a closure to capture that uniquely for each time through the for loop:

http = require('http');

function download(name, callback) {
    //name is an array whit csgo items names.
    for (var i = 0; i < name.length; i++) {

        var marketHashName = getGoodName(name[i]);
        // create closure to capture marketHashName uniquely for each 
        // iteration of the for loop
        (function(theName) {
            var url = 'http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=' + marketHashName;

            http.get(url, function (res) {
                var data = "";
                res.on('data', function (chunk) {
                    data += chunk;
                });
                res.on("end", function () {

                    data = JSON.parse(data);
                    var value= 0;
                    //get the value in the json array
                    if(data.median_price) {
                        value = data.median_price;
                    }else{
                        value = data.lowest_price;
                    }

                    value = value.substr(5);
                    console.log("WEAPON",value);

                    // now that the async function is done, call the callback
                    // and pass it our results
                    callback(theName, value, data);
                });
            }).on("error", function () {

            });
        })(marketHasName);
    }
}

// sample usage:
download("whatever", function(name, value, data) {
   // put your code here to use the results
});

FYI, you may find that the request module which is a higher level set of functionality on top of the http module will save you some work.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Whats is this method/way of doing it called if i wan't to learn more about it. Or is it callbacks 101?
@user2034253 - it's just asynchronous callbacks.
@user2034253 - FYI, if you want to read more about handling asynchronous operations, you may want to read about promises. I use the Bluebird promise library for my node.js development and find it much easier than just callbacks, particular when trying to coordinate multiple async operations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.