【发布时间】:2018-05-27 22:58:33
【问题描述】:
即
async asyncfunction(){
try{
await method1();
await method2();
}
catch(error){
console.log(error);
}
}
给定 method1() 和 method2() 是异步函数。每个 await 方法是否应该有一个 try/catch 块?有没有更干净的方法来写这个?我试图避免 '.then' 和 '.catch' 链接。
【问题讨论】:
-
这取决于您是否需要
catch来执行不同的操作,具体取决于哪个函数导致错误。一次 try/catch 通常就可以了 -
我发现这篇文章很有帮助。除其他方面外,它还涉及这种用法的含义。 dev.to/maxart2501/gotchas-about-asyncawait-and-promises-9di