I am trying to return the array which is being async populated after every foreach
loop. But when I return, I get only undefined
.
Below is the code block:
var navigateAndFetchPages = function (data) {
var countryPages = [];
data.forEach(function (val) {
Rq(val.esomar_url)
.then(function (data) {
var $ = cheerio.load(data),
pages_elem = $('.mt0.mb0-5.pt0').find('a').not('.active');
countryPages.push({country_name: val.country_name, links: pages_elem});
})
});
return countryPages;
};
var scraper = {
extract: function (dir) {
return landingPage(dir)
.then(function (countries) {
return navigateAndFetchPages(countries)
})
.then(function (p) {
p()
})
.catch();
}
};