为什么try catch能捕捉await后promise错误? 和执行栈有关系吗?
· 3 min read
问题
为什么try catch能捕捉await后promise错误? 和执行栈有关系吗?
为什么try catch 中promise的错误catch不了。 await后就可以catch了?
普通函数不能try catch promise,因为其throw error时的执行栈,和try catch它的函数无关了。
而async await能像generate一样在控制执行流,触发时,函数执行栈恢复到 try catch它时的函数执行栈,所以能try catch。