admin管理员组文章数量:1026989
I have the bit of javascript below. Using async/await in our ES6 project. I have noticed that now all of a sudden a 404 response code isn't hitting the catch. In fact the .json() is also throwing a console error but still not hitting the catch. I would expect any error in the try to immediately throw and go to the catch block of code.
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();
}
}
I have the bit of javascript below. Using async/await in our ES6 project. I have noticed that now all of a sudden a 404 response code isn't hitting the catch. In fact the .json() is also throwing a console error but still not hitting the catch. I would expect any error in the try to immediately throw and go to the catch block of code.
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();
}
}
Share
Improve this question
edited Sep 28, 2016 at 16:01
Felix Kling
818k181 gold badges1.1k silver badges1.2k bronze badges
asked Sep 28, 2016 at 15:57
allencodedallencoded
7,30517 gold badges75 silver badges133 bronze badges
1
-
1
FYI,
async/await
is not part of ES6. It will (most likely) be part of the spec released next year. – Felix Kling Commented Sep 28, 2016 at 16:02
1 Answer
Reset to default 6If the json()
method is asynchronous, you should add one more 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();
}
}
I have the bit of javascript below. Using async/await in our ES6 project. I have noticed that now all of a sudden a 404 response code isn't hitting the catch. In fact the .json() is also throwing a console error but still not hitting the catch. I would expect any error in the try to immediately throw and go to the catch block of code.
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();
}
}
I have the bit of javascript below. Using async/await in our ES6 project. I have noticed that now all of a sudden a 404 response code isn't hitting the catch. In fact the .json() is also throwing a console error but still not hitting the catch. I would expect any error in the try to immediately throw and go to the catch block of code.
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();
}
}
Share
Improve this question
edited Sep 28, 2016 at 16:01
Felix Kling
818k181 gold badges1.1k silver badges1.2k bronze badges
asked Sep 28, 2016 at 15:57
allencodedallencoded
7,30517 gold badges75 silver badges133 bronze badges
1
-
1
FYI,
async/await
is not part of ES6. It will (most likely) be part of the spec released next year. – Felix Kling Commented Sep 28, 2016 at 16:02
1 Answer
Reset to default 6If the json()
method is asynchronous, you should add one more 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();
}
}
本文标签: javascriptAsyncAwait JS Catch not working in trycatchStack Overflow
版权声明:本文标题:javascript - AsyncAwait JS Catch not working in trycatch - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745663822a2162055.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论