【发布时间】:2018-03-01 04:21:13
【问题描述】:
我在 react-native 中使用 fetch 来进行 API 调用。
我需要获取状态码(200、401、404)和响应数据。
获取响应数据的工作:
return fetch(url)
.then(response => {
return response.json();
})
.then(res => {
console.log("reponse :", res); // <-------- res is ok with data
}) .catch(error => {
console.error(error);
return { name: "network error", description: "" };
});
现在我调整第一个然后获取状态码,但数据不是我除了
return fetch(url)
.then(response => {
const statusCode = response.status;
const data = response.json();
return { statusCode, data };
})
.then(res => {
console.log("reponse :", res); // <-------- i get a "promise"
}).catch(error => {
console.error(error);
return { name: "network error", description: "" };
});
控制台日志:
{statusCode: 200, data: Promise}
【问题讨论】:
标签: react-native promise fetch