4

我在下面有一点 javascript。在我们的 ES6 项目中使用 async/await。我注意到现在突然之间没有 404 响应代码。事实上 .json() 也抛出了一个控制台错误,但仍然没有抓住问题。我希望尝试中的任何错误都会立即抛出并转到代码的 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();
  }
}
4

1 回答 1

5

如果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();
  }
}
于 2016-09-28T15:59:51.920 回答