What am I missing?
try {
} catch (error){
}
My understanding is that when a code in the try block throws an error, then the program loops through the code in the catch block and the error is captured in the error identifier.
Now consider this code snippet
async function hostDinnerParty(){
try {
let meal = await cookBeanSouffle();
console.log(`${meal} is served!`);
} catch(error){
console.log(error);
console.log('Ordering a pizza!');
}
}
When I call hostDinnerParty()
, cookBeanSouffle()
returns a promise that can randomly either reject or resolve! When it resolves the try block runs, but when it rejects the catch block runs telling me indirectly that the reject state of the promise is indeed similar to an error being thrown as only when we throw error we can loop through the catch block.
Before this I never looked at the reject state of a promise being equivalent to an error. Is my understanding of this correct?
What am I missing?