In the below code why promise.then()
behaves asynchronously. In other words , why browser is not waiting for the code written inside promise.then()
method to be executed?.What tell the browser engine so that promise.then()
makes an asynchronous call?
const money = 500;
let promise = new Promise(function(resolve,reject){
if(money > 400){
resolve('You have a car!');
}else{
reject('Yo do not have enough money to buy the Car!');
}
});
console.log('Before');
promise.then(function(data){
console.log('Success '+data);
});
console.log('After');
The above code prints the output in the below order, which means promise.then() works asynchronously.
- Before
- After
- Success You have a car!