I’m using the following code to make HTTP requests to my API:
fetch('api/register', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(jsonObj)
})
.then(response => {
responseClone = response.clone();
if (!response.ok) {
console.log(`HTTP ERROR STATUS CODE: ${response.status}`);
}
return response.json();
})
.then(function (data) {
processDataFromServer(data);
}, function (rejectionReason) {
console.log('JSON Parsing Error:', rejectionReason, responseClone);
responseClone.text()
.then(function (bodyText) {
console.log('Cannot parse as JSON:', bodyText);
});
})
.catch(error => {
console.log("Error: " + error)
deplayError(error);
});
The code works! The only change I want to do is to pass response.status
to my processDataFromServer(data)
function. Something like processDataFromServer(data, response.status)
.
Since response.json()
is a promise, I cannot return a promise and a property at the same time to my next method. Any Idea how it can be done?