在调用函数时是否有忽略异步错误的方法?
同步
function synchronous() {
console.log('Nice feature');
throw new Error('Async Error');
}
try {
synchronous();
console.log('Succeeded');
} catch (e) {
console.log('Caught');
}
控制台输出:Nice feature
Caught
异步
async function asynchronous() {
console.log('Nice feature');
throw new Error('Async Error');
}
try {
asynchronous();
console.log('Succeeded');
} catch (e) {
console.log('Caught');
}
控制台输出:Nice feature
Uncaught (in promise) Error: Async Error at asyncFunc
两个选项:
(async () => {
try {
await asynchronous();
console.log('Succeeded');
} catch (e) {
console.log('Caught');
}
});
或
asynchronous()
.then(r => {
console.log('succeeded');
})
.catch(e => {
console.log('caught');
});
我不确定这是否回答了你的问题,但如果"等待";对于调用asynchronous((函数的结果,不会发生错误