Axios Retry Interceptor With Exponential Backoff

Implement response-layer retry in Axios via a custom interceptor with attempt counters, exponential backoff, and idempotency-aware opt-in.

JavascriptResilience

Code

javascriptsnippet
31 lines
import axios from 'axios';

const client = axios.create({
  baseURL: 'https://api.example.com',
  timeout: 8000,
  headers: { 'x-client': 'batch-sync/1.2' },
});

client.interceptors.response.use(
  (res) => res,
  async (error) => {
    const config = error.config ?? {};
    const retries = config.__retries ?? 0;
    const idempotent = ['GET', 'HEAD', 'PUT', 'DELETE'].includes(config.method);

    const transient = error.response
      && [429, 502, 503, 504].includes(error.response.status);

    if (!idempotent || !transient || retries >= 3) {
      return Promise.reject(error);
    }

    config.__retries = retries + 1;
    const delay = 2 ** config.__retries * 500;
    await new Promise((r) => setTimeout(r, delay));
    return client(config);
  },
);

const data = await client.get('/catalog?page=2');
console.log('items:', data.data.items.length);

What the code does

The asynchronous flow is linearized with await, keeping failure handling local.

The demo data lives in module-level bindings: client, data.

What to watch for

  • External dependency: axios — install it before running the snippet.
  • The guard !idempotent || !transient || retries >= 3 short-circuits the flow with an early return when its condition holds.

Applying it

Running the file prints labeled results such as items: — each operation’s effect is visible in the output.

Statements that matter

await new Promise((r) => setTimeout(r, delay));
return Promise.reject(error);
const data = await client.get('/catalog?page=2');

Interactive Tools for This Topic

FAQ

Are there third-party dependencies?
`axios` — install it with the package manager before running.