With the following code:
fetch(url, params).then(response => {
return response.json();
}).then(json => {
// json handler
if (!response.ok) { // <-- Does not work, I don't have access to response object here
if (json.error)
throw new Error(json.error); // Application spesific error
else
throw new Error(`HTTP error ${response.statusText}(${response.status})`); // Generic error
}
console.log('Success:', json);
}).catch((e) => {
console.error("Failed with:", e);
});
I have a problem where I can't get access to the response object from the json handler. I can't store response in a global variable because obviously that is a bad idea for many reasons. I can't use this as it simply points to window.
So what would be the proper way of passing the response object along to the then() handler of the json() promise in this case?