all*_*ded 4 javascript asynchronous try-catch async-await ecmascript-2017
我在下面有一些 javascript。在我们的 ES6 项目中使用 async/await。我注意到现在突然间 404 响应代码没有命中。事实上 .json() 也抛出了一个控制台错误,但仍然没有命中。我希望 try 中的任何错误立即抛出并转到代码的 catch 块。
async getDash(projectId, projectUserId) {
try {
const events = (await this.apiHttp
.fetch(`${projectId}/users/${projectUserId}/participant-event-dash`)).json();
return events;
} catch (e) {
// fail back to local (dev testing)
return (await this.http
.fetch(`${this.appConfig.url}dist/api/query/json/partic-event-dash.json`)).json();
}
}
Run Code Online (Sandbox Code Playgroud)
如果该json()方法是异步的,您应该再添加一个await:
async getDash(projectId, projectUserId) {
try {
const events = await (await this.apiHttp
.fetch(`${projectId}/users/${projectUserId}/participant-event-dash`)).json();
return events;
} catch (e) {
// fail back to local (dev testing)
return await (await this.http
.fetch(`${this.appConfig.url}dist/api/query/json/partic-event-dash.json`)).json();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2602 次 |
| 最近记录: |