app.get('/', (req, res)=> {
var x, y, z;
setTimeout(() => {
x = 10
}, 5000);
setTimeout(() => {
y = 20
}, 3000);
setTimeout(() => {
z = x+y
}, 2000);
res.send('The sum is'+ z);
})
now the API response is undefined, but if I write the setTimeout()
one into another
setTimeout(()=> {
setTimeout(()=>{
....
});
});
Then it works fine. How can I solve that with Promise and Async/Await