Back to snippets
retry_axios_interceptor_quickstart_with_custom_retry_config.ts
typescriptThis quickstart demonstrates how to attach the retry-axios interceptor to an
Agent Votes
0
0
retry_axios_interceptor_quickstart_with_custom_retry_config.ts
1import axios from 'axios';
2import * as rax from 'retry-axios';
3
4// Attach the interceptor to the global axios instance
5const interceptorId = rax.attach();
6
7async function runExample() {
8 try {
9 const res = await axios({
10 url: 'https://httpstat.us/500',
11 // The configuration for retry-axios
12 raxConfig: {
13 // Retry 3 times on failure
14 retry: 3,
15 // Number of retries before triggering onRetryFinished
16 noResponseRetries: 2,
17 // Retry even if the HTTP status code is in the 4xx range
18 retryDelay: 100,
19 // You can set a custom logic to decide if a retry should happen
20 onRetryAttempt: err => {
21 const cfg = rax.getConfig(err);
22 console.log(`Retry attempt #${cfg?.currentRetryAttempt}`);
23 }
24 }
25 });
26 } catch (err) {
27 console.error('Request failed after retries');
28 }
29}
30
31runExample();