Created
January 31, 2020 00:09
-
-
Save mkiddTempus/4ce156382c1b654b34d44d1900a37d2b to your computer and use it in GitHub Desktop.
This file contains 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 rax = require('retry-axios'); | |
const axios = require('axios').default; | |
function sleep(ms) { | |
return new Promise((resolve) => setTimeout(resolve, ms)); | |
} | |
async function makeAPICallMethod2(){ | |
try{ | |
console.log('started'); | |
// # Method 2 - new instance, does not override defaults for retry, retryDelay, or onRetryAttempt | |
const axiosInstance = axios.create(); | |
axiosInstance.defaults.raxConfig = { | |
instance: axiosInstance, | |
retry: 10, | |
retryDelay: 750, | |
onRetryAttempt: err => { | |
const cfg = rax.getConfig(err); | |
console.log(`Retry attempt #${cfg.currentRetryAttempt}`); | |
} | |
}; | |
const interceptorId = rax.attach(axiosInstance); | |
console.log({interceptorId}); | |
const res = await axiosInstance.put('https://httpstat.us/500'); | |
} catch (error) { | |
console.log('Caught an error'); | |
console.log(error); | |
} | |
} | |
module.exports = {makeAPICallMethod1, makeAPICallMethod2, makeAPICallMethod3}; | |
// =============== | |
// node -p "require('./axios-test').makeAPICallMethod1()" | |
// node -p "require('./axios-test').makeAPICallMethod2()" | |
// node -p "require('./axios-test').makeAPICallMethod3()" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment