Skip to content

Instantly share code, notes, and snippets.

@1n3ffbl3
Last active October 19, 2018 14:25
Show Gist options
  • Save 1n3ffbl3/016c5ad552a75ebe7d9c1289f45613dc to your computer and use it in GitHub Desktop.
Save 1n3ffbl3/016c5ad552a75ebe7d9c1289f45613dc to your computer and use it in GitHub Desktop.
Piece of code
async function getStarWarsHeroes() {
let retryLimit = 3;
let success = false;
// API is not always returning data (500 erros happens) so we extend your tries to 3 times that explain a for loop
for (let it = 0; it < retryLimit; ++it) {
let data = await axios.get('...')
.then(response => response.json())
.then(json => {
success = true;
return json;
}
.catch(error => {
console.error(error);
})
if (success === true) {
return data;
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment