I'm somewhat new to javascript. Recently, I've been working on a node.js project, and I came across the problem of asynchronous callbacks in javascript. My code is below:
So ideally, I would like the request method to be caried out, THEN the callback from the res.on('data') event to be executed, and finally have callback for the .each() iterator to be called. Unfortunately, the asynchronous nature of javascript causes the program to exit the main code block even before the first callback is executed.
var req = https.request(options, function(res) {
// console.log("statusCode: ", res.statusCode);
// console.log("headers: ", res.headers);
console.log('1st call back exec');
res.on('data', function(d) {
console.log('2st call back exec');
var $ = cheerio.load(d);
//traversing method
$('.dddefault').each(function(i){
console.log('3st call back exec');
if(i == 3){
remainingSeats = parseInt($(this).text().trim());
console.log('inner0:'+remainingSeats);
return;
}
});
console.log('inner1:'+remainingSeats);
});
console.log('inner2:'+remainingSeats);
});
req.end();
req.on('error', function(e) {
console.error(e);
});
Can anyone point me in the right direction to get my code working in the way I had intended it to work? Thanks.