I'm currently scraping data from a webpage and then pushing it to an array, the code currently looks like this
url = //url
let data = [];
request(url, function (err, response, html) {
if (!err) {
var $ = cheerio.load(html);
$('#id').each(function (i, element) {
data_element = //(this).find...
data.push(data_element);
});
}
console.log(data); //console logs the data inside the request
})
console.log(data); //logs empty array outside of request
The data is logged when I call console log inside the request, but if I call it outside of the request function then it returns an empty array. I know I need to use a callback function but I was wondering what the best way to go about this is, as I will be making multiple requests inside my function.