Используя async / await, я попробовал два разных синтаксиса:
async function asyncFunc() {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject('promise rejected')
}, 500);
});
}
async function asyncCallWithoutTryCatchBlock() {
await asyncFunc().catch((err) => {
console.log('Catching error');
return;
});
console.log('Outside, after catch is called');
}
async function asyncCallWithTryCatchBlock() {
try {
await asyncFunc();
} catch(err) {
console.log('Catching error');
return;
}
console.log('Outside, after catch is called');
}
asyncCallWithoutTryCatchBlock();
asyncCallWithTryCatchBlock();
Я ожидаю этого вывода:
Catching error
Catching error
Я получаю это:
Catching error
Outside, after catch is called
Catching error
Мне интересно, почему внешний console.log
вызывается в asyncCallWithoutTryCatchBlock
, так как я делаю явный возврат в блоке catch
?