0

I'm currently writing my first "real" nodeJS package, and I'm having much troubles handling the async aspect of the language.

I have a request that grabs some html, parse it (with cheerio) and return all the link tags of the page. But this is quite a computing, and the return is triggered too soon, so my variable doesn't have the time to populate with my links.

Here is my code :

crawl.linksOfThePage = function(url) {
    var aTags
      , urls = [];

    request(url, function(error, response, body){
        if (!error && response.statusCode == 200) {
            // Load the body
            var $ = cheerio.load(body);

            // Get all links
            aTags = $('a');
            for (var i = 0; i< aTags.length; i++) {
                urls.push(aTags[i].attribs.href);
            };
        }
    });

    return urls;
}

Any help to fix this would be great, but in a more general topic, I really struggle to understand how to "return" stuff in node, I feel like I can juste "console.log", and nevery really count on my variables aviability. Any advices / links / article to help a new async programmer ?

Thanks for your help :)

tmos
  • 323
  • 1
  • 3
  • 13

1 Answers1

-1

You need to return a promise of the urls.

djechlin
  • 59,258
  • 35
  • 162
  • 290