I want to use async/awayt syntax, Fetch API and want to achieve the following behavior:
if the response is not 200, log the response, don't throw anything and return null. if the response is 200, return the response.
But! Fetch API throws an exception for everything that is different from 404, 505 or 200 and in the end I get an ugly construction like this:
...
try{
let response = await fetch(url, {method: 'GET', mode: 'cors'});
if(response.status != 200){
console.log('Failed to get what I want, got status: ' + response.status);
return null;
}
catch(e){
console.log('error bla bla');
return null;
}
...
Is there a more elegant way to achieve the same?
async
declaration?Fetch API throws an exception for everything that is different from 404, 505 or 200
-- Fetch is only supposed to reject for network errors not based on status code.