I use fetch in react-native to make API calls.
I need to get status code (200 , 401, 404 ) and the response data.
This work to get the response data :
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: "" };
});
Now i tweak the first then to get the status code but data is not what i except
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: "" };
});
the console log :
{statusCode: 200, data: Promise}