Sometimes I find myself writing the following code:
if (result.status == 200) {
return result.json()
} else {
return result.json().then(res => Promise.reject(res))
}
If the status is any other than 200 then the result will contain an error message which I would like to return. (This is part of a api.js script)
Is there a better way to "inverse" the .json() promise?
I thought of maybe something like this:
return Promise.reject(result.json)
Is there a way for me to do something like that? I am expecting the successful result of result.json to be the error object in the catch route.