Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

240
Vistas
Rerun a function on error but with a delay that doubles every time new error thrown javascript react

I have a somewhat unreliable REST API which I'm calling (around 20% of the time it throws a 502 error). I want to be able to catch the error, then rerun the function, but after a delay. If the error occurs again I want to double the original delay and have it rerun, if it occurs again repeat those steps with the delay doubled again and so on. I think I would use a setTimeout function with the delay being a variable which changes and then clearTimeout on success. My issue is I don't know how I would implement this, and specifically I don't know how I would rerun that exact API call which threw the error. My code is below, any help is much appreciated.

Also this is for a NextJS React app if that changes anything.

const allData = async () => {
    const dates = getArrayOfDates('START_DATE', 'END_DATE');
    let dataStructure = {
        "country_name": "",
        "iso": "",
        "data": {
        }
    }   
    dates.map(async (date) => {
        options = {
            method: 'GET',
            url: 'https://covid-19-statistics.p.rapidapi.com/reports',
            params: {
              iso: 'USA',
              date: date
            },
            headers: {
              'x-rapidapi-host': 'covid-19-statistics.p.rapidapi.com',
              'x-rapidapi-key': 'API_KEY'
            }
          };
          let cases = 0;
          let deaths = 0;
          let dateV;
          await axios.request(options).then(response => {
            dateV = response.data.data[0].date;
            const data = response.data.data;
            data.map(item => {
                cases += item.confirmed;
                deaths += item.deaths;
            });
            })
            .then(()=> {
                console.log("DATE", dateV, "cases", cases, "deaths", deaths)
                dataStructure.country_name = "US";
                dataStructure.iso = "USA";
                dataStructure.meta_data[dateV] = {
                    "cases": cases,
                    "deaths": deaths,
                    "date": dateV
                }
            })
            .catch(error => {
                console.error(error);
            });
    })
}
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda