Skip to content

Instantly share code, notes, and snippets.

@serkanh
Created May 24, 2018 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save serkanh/ba00aca868cc61cc2964754cfed1e2b8 to your computer and use it in GitHub Desktop.
Save serkanh/ba00aca868cc61cc2964754cfed1e2b8 to your computer and use it in GitHub Desktop.
retry pattern with await
const axios = require('axios');
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function fetch_retry(url,n){
try{
const data = await axios(url);
return data;
}catch(err){
if (n === 1) throw err;
console.log('failed!')
await sleep(3000);
fetch_retry(url, n - 1)
.catch((err)=>console.log(err))
}
}
fetch_retry('https://www.google.com/sdfa',3)
.catch((err)=>{
console.log('error output==> ',err);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment