I have the following function that uses fetch:
function requests(url) {
return fetch(url)
.catch(function (error) {
return null;
})
.then(function (response) {
if (response == null) {
return null;
} else if (!response.ok) {
if (response.status == 422) {
// HIT No longer exists
return null;
} else if (response == 429) {
//too many requests
return null;
}
console.log("caught error: " + response.status);
return null;
}
js = response.json();
console.log(js);
return js;
});
}
My problem is that occasionally the fetch is 'rejected'. None of the code catches that error until I try to read 'response.json'. I cannot find how to access the 'PromiseState' in 'js'. When I log "js" I get this:
Promise
[[Prototype]]: Promise
[[PromiseState]]: "rejected"
[[PromiseResult]]: SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
Can someone help me with this?