Home > Enterprise >  How to call api multiple times with delay in axios till I receive correct response?
How to call api multiple times with delay in axios till I receive correct response?

Time:06-02

Call an API multiple times with delay till I receive appropriate response.

Server needed more time to process an excel sheet information with 2000 rows.

CodePudding user response:

You might be looking for axios-retry. Example from the readme:

import axiosRetry from 'axios-retry';

axiosRetry(axios, { retries: 3 });

axios.get('http://example.com/test') // The first request fails and the second returns 'ok'
  .then(result => {
    result.data; // 'ok'
  });

CodePudding user response:

you can simply use axios interceptor like so:

axiosInstance.interceptors.response.use(
    (response) => response,
    (error) => {
        const originalRequest = error.config;

        if (error.response.status === 400) {
            originalRequest._retry = true;
           
        }
})

error param is when you want to deal with errors and response is where you want to deal with responses from backend

  • Related