it should be understood that the < del > rejection occurs in the future < / del > callback will only be executed in the future event loop.
function f2() {
try {
Promise.reject('').catch(err => {
console.log('2', err)
});
console.log('1')
} catch (e) {
console.log(e)
}
}
try..catch structure, which can only be Synchronize, can not be used in asynchronous code mode
to answer this question, the principle or explore what exactly await does?
await: means pausing the asynchronous function promise, to wait for the execution of any expression.
for the f2 function, Promise.reject ('error') executes asynchronously, but there is no catch function for reject processing.
for the f function, await Promise.reject ('something went wrong'). It is equivalent to adding a catch processing method to the end, which returns the incoming 'error' information, so it does not report an error.
because
1, try catch cannot catch asynchronous code errors
2, Promise.reject is an asynchronous method
21st century, use native async and promise