Created
May 24, 2018 18:05
-
-
Save serkanh/ba00aca868cc61cc2964754cfed1e2b8 to your computer and use it in GitHub Desktop.
retry pattern with await
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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