0

I'm new on Node.js, I want to scrap some data from other website that no legal issue related, But there is some problem because of Node.js's asynchronous. Please see below code that request to some url and parsing document using Cheerio.js.

function getPrices() {
    var url = "http://some-price-list-url.com";

    request(url, function (err, res, html) {
        var $ = cheerio.load(html);
        var prices = {};

        for(var i=0; i<10; i++){
            prices[i] = $(span[class="someSelector"]); 
        }

        return prices;

    });

}

No problem with above code, So I want to display returned object, so

var prices = getPrices();

console.log(prices); // ---> undefined 

I want to display to console the returned object, but it shown undefined. I test same structure code without stream task, It works well. So I guess the console.log tried before getPrices() not yet completed. How can I show this object properly?

ton1
  • 7,238
  • 18
  • 71
  • 126
  • Why can't you just log `prices` inside the callback function? – Maria Ines Parnisari Apr 13 '16 at 02:25
  • @mparnisari The log is not real purpose, anyway I found the solution. adding to for recursion :`if(i == 9){callback(prices); return prices; }` – ton1 Apr 13 '16 at 02:27
  • and then display prices this way : `getPrices('005930', function(prices){ console.log(prices); })`. I come up the solution writing a question...Sorry – ton1 Apr 13 '16 at 02:28
  • It happens all the time. See related question: http://stackoverflow.com/questions/23339907/returning-a-value-from-callback-function-in-node-js – Maria Ines Parnisari Apr 13 '16 at 02:29
  • 3
    Asynchronous code needs to be handled with some form of [callback function](http://stackoverflow.com/questions/9596276/how-to-explain-callbacks-in-plain-english-how-are-they-different-from-calling-o). What you're trying to do is a perfect example of *synchronous* code – Nick Zuber Apr 13 '16 at 02:29

1 Answers1

0

Try something like this:

var http = require('http');

http.get('http://some-price-list-url.com', function(response)
{
    response.on('data', function(data)
    {
        console.log(data);
    });
});