【发布时间】:2020-09-17 23:43:39
【问题描述】:
我尝试检查我的请求的状态是否为 200(OK),但我不知道如何一起做这些事情,因为第一个和第二个 .then 不是“彼此相似”:
function f(path) {
await fetch(path)
.then(response => {
// console.log(response.status);
if (response.status != 200) {
throw response.status;
} else {
// do something
}
})
.then(response => response.json())
.then(...method for the response.json()...)
.catch(error => {
// print some error message
}
}
- 然后第二个失败并返回错误。
我扔的时候有问题。
它将错误打印到控制台(当我通过将路径更改为错误路径进行检查并且我想看看我是否处理错误时)。
我能做什么?
【问题讨论】:
-
请添加一个minimal reproducible example(至少包括抛出的错误,并根据错误响应的内容)显示实际问题
-
您需要在第一个
then中返回response,以便第二个then能够使用它
标签: javascript json fetch fetch-api