Reputation: 459
I have this Ajax request function.
export async function fetchAllCoinsPromise() {
const apiCall = await fetch('https://api.coincap.io/v2/assets');
const jsonResult = await apiCall.json();
return jsonResult;
}
I call it like this:
const fetchCoins = () => {
fetchAllCoinsPromise()
.then(res => console.log(111))
.catch(err => {
console.log('err', err);
fetchCoins();
});
};
the reason why I'am doing this recursion is because sometimes I'm getting this strange error: SyntaxError: JSON Parse error: Unexpected identifier "You"]
and I thought the async recursion would solve the issue and it does but I am not sure if this is the best solution in case of time complexity. Any suggestions please?
Upvotes: 2
Views: 1164
Reputation: 56
You can use a filter if you want. It might be a hacky solution but it works.
An example might look like:
if(jsonResult.startsWith('Y')){
console.log("Error: " + jsonResult);
tryYourCallAgain();
} else {
return jsonResult;
}
You could use any filter that would catch the error like typeof, includes, etc. Whatever is best in your case.
Pardon if this code is sloppy as I'm relatively new to Javascript, but this worked for me in a similar case. Hope it helps!
Upvotes: 4