I'm facing a problem with callback functions and I can't find a solution for that. I need to get one value inside a callback to compare lately. The problem is, when I compare, my variable is still with initial value.
router.get('/qadashboard', (req, res) => {
var total = -1;
var options = {
method: 'GET',
uri: 'https://myurl.com/users',
json: true
};
request(options)
.then((response) => {
// Get Total
total = response.body.total;
})
.catch((err) => {
console.log('API Error - ', err);
});
if (total < 10) {
// Code here
} else {
// Code here
}
res.render("index");
});
Total is always -1 and I am sure that response.body.total is not -1 (always return positive numbers). If I code console.log(response.body.total) inside the callback function it's returning the right number. Is there any way that I can wait till callback execution is finish and later on compare if total < 10?
Thank you