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