【发布时间】:2021-12-30 17:08:54
【问题描述】:
有没有办法在调用函数时忽略异步错误?
同步
function synchronous() {
console.log('Nice feature');
throw new Error('Async Error');
}
try {
synchronous();
console.log('Succeeded');
} catch (e) {
console.log('Caught');
}
控制台输出:Nice featureCaught
异步
async function asynchronous() {
console.log('Nice feature');
throw new Error('Async Error');
}
try {
asynchronous();
console.log('Succeeded');
} catch (e) {
console.log('Caught');
}
控制台输出:Nice featureUncaught (in promise) Error: Async Error at asyncFunc
【问题讨论】:
-
您应该了解
await、Promise.prototype.catch和可选的全局错误处理程序,它们可以捕获您否则忘记捕获的所有内容。
标签: javascript asynchronous error-handling