让我们想象一下,我有一个这样的场景:
async function some_func() {
await some_query.catch(async (e) => {
await some_error_code()
})
console.log('The above function ran without an error')
}
我希望只有异步函数成功运行时,console.log()
才会被执行。目前,我的解决方案是:
async function some_func() {
let did_error = false
await some_query.catch(async (e) => {
await some_error_code()
did_error = true
})
if (did_error) return
console.log('The above function ran without an error')
}
但这不是很好。有没有不增加代码量的处理方法?类似于多个for循环:
outer: for (let i = 0; i < 10; i++) {
for (let j = 0; j < 10; j++) {
continue outer;
}
console.log('Never called')
}
then
而不是catch
吗?请看使用Async/Await的正确Try...Catch语法。 - Bergi