0

I'm trying to return the recipes array, however it seem to be empty. i guess that is because the callback is being executed before the loop is being executed. How can i fix this in this case where i loop with cheerio?

function scrapeNow(url, callback) {

  request(url, function(error, response, html){

    // First we'll check to make sure no errors occurred when making the request
    if(!error){
      var recipes = [];
      var $ = cheerio.load(html);

      $('div.article-block a.picture').each(function(i, elem) {

        console.log(i);
        var deepUrl = $(this).attr('href');

        if(!$(this).attr('href').indexOf("tema") > -1) {
          request(deepUrl, function(error, response, html){

            // First we'll check to make sure no errors occurred when making the request
            if(!error){

              var $ = cheerio.load(html);

              var image = $('div.article div.article-main-pic img').attr('src');
              var title = $('div.recipe h2.fn').text();

              var object = {url: deepUrl, title : title, image : image};

              recipes.push(object);

            }

          });

        }

      });
      callback(recipes);
    }

  });


}
Peter Pik
  • 11,023
  • 19
  • 84
  • 142
  • I already answer you :D. But put callback after second if(!error){} inside second request and you will be good – Mykola Borysyuk Sep 15 '16 at 22:48
  • Yes but if i do like you and put callback in there it will loop multiple times which is not ideal when i want to `res.json`it. – Peter Pik Sep 15 '16 at 22:59
  • Are you using this code on front-end? Because i dont notice it previously.. Hm let me think – Mykola Borysyuk Sep 15 '16 at 23:19
  • Check this library http://caolan.github.io/async/ and check this answer http://stackoverflow.com/questions/15504921/asynchronous-loop-of-jquery-deferreds-promises .. Maybe this will help – Mykola Borysyuk Sep 15 '16 at 23:23

1 Answers1

0

i guess that is because the callback is being executed before the loop is being executed.

Nop, it's because in your loop you have an other request

request(deepUrl, function(error, response, html){

and recipes is filled in the complete callback (anonymous function) of this request.

Put callback(recipes) in this complete callback.

Robiseb
  • 1,576
  • 2
  • 14
  • 17