Ivá*_*hez 5 javascript ecmascript-2017
在异步函数中将错误抛出到较高级别
这个
async create(body: NewDevice, firstTry = true): Promise<RepresentationalDevice> {
try {
return await this.dataAccess.getAccessToken()
} catch (error) {
throw error
}
}
Run Code Online (Sandbox Code Playgroud)
VS这个
async create(body: NewDevice, firstTry = true): Promise<RepresentationalDevice> {
return await this.dataAccess.getAccessToken()
}
Run Code Online (Sandbox Code Playgroud)
我的意思是在最后一级我必须抓住错误,并且在捕获时根本没有任何修改
这两种方法是否相同?我可以使用第二种方法而不会出现错误处理问题吗
这与异步函数无关。捕获错误只是为了重新抛出它与一开始就没有捕获它是一样的。IE
try {
foo();
} catch(e) {
throw e;
}
Run Code Online (Sandbox Code Playgroud)
和
foo();
Run Code Online (Sandbox Code Playgroud)
基本上是等价的,除了堆栈跟踪可能不同(因为在第一种情况下错误是在不同的位置抛出的)。
| 归档时间: |
|
| 查看次数: |
652 次 |
| 最近记录: |