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

232
Vistas
How to call an API twice if there is an error occurred?

I have an internal API that I would like to post data. Depends on some cases, I am seeing errors. So what I would like to do is to call it again if there is an error occurred.

What I did was to create a counter to pass it to the function and call the function recursively as below. This gives me the error as below:

UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block or by rejecting a promise which was not handled with .catch(). (rejection id: 1)

Here is how I call the function:

....
private RETRY_API = 1;
....

 try {
    await this.callAPI(request, this.RETRY_API);
} catch (error) {
    console.log('error', error);
}

This program never comes to the catch block above.

And here is my actual function that I call the API:

private async callAPI(request, retry) {
    return new Promise((resolve, reject) => {
       someService.postApiRequest('api/url', request, async(err: any, httpCode: number, data) => {
     if (this.RETRY_API == 2) {
         return reject(err);
      } else if (err) {
          this.callAPI(request, retry);
          this.RETRY_API++;
      } else if ( httpCode !== 200 ) {
          this.RETRY_API = 2;
          // some stuff
      } else {
           this.RETRY_API = 2;
           // some stuff
           return resolve(data);
      }
   });
  })
}

Not sure what I am missing. If there is a better way to call the API twice if an error occurred, that would be great if you let me know.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Let's organize a little differently. First, a promise-wrapper for the api...

private async callAPI(request) {
  return new Promise((resolve, reject) => {
    someService.postApiRequest('api/url', request,(err: any, httpCode: number, data) => {
      err ? reject(err) : resolve(data);
    });
  });
}

A utility function to use setTimeout with a promise...

async function delay(t) {
  return new Promise(resolve => setTimeout(resolve, t));
}
  

Now, a function that calls and retries with delay...

private async callAPIWithRetry(request, retryCount=2, retryDelay=2000) {
  try {
    return await callAPI(request);
  } catch (error) {
    if (retryCount <= 0) throw err;
    await delay(retryDelay);
    return callAPIWithRetry(request, retryCount-1, retryDelay);
  }
}

If you can't force a failure on the api to test the error path some other way, you can at least try this...

private async callAPIWithRetry(request, retryCount=2, retryDelay=2000) {
  try {
    // I hate to do this, but the only way I can test the error path is to change the code here to throw an error
    // return await callAPI(request);
    await delay(500);
    throw("mock error");
  } catch (error) {
    if (retryCount <= 0) throw err;
    await delay(retryDelay);
    return callAPIWithRetry(request, retryCount-1, retryDelay);
  }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Try replacing

if (this.RETRY_API == 2)

with

if (this.RETRY_API > 1)
about 4 years ago · Juan Pablo Isaza Denunciar

0

It looks like you need to add return await to the beginning of the line this.callAPI(request, retry); in callAPI function.

Similarly there are some condition blocks that doesn't resolve or reject the promise. While it might work okay, it's considered bad practice. You want to either resolve or reject a promise.

about 4 years ago · Juan Pablo Isaza Denunciar
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