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

172
Vistas
How to use async.js libary with an await?

I'm trying to implement the async library so that it polls an API for a transaction until one of them succeeds.

router.get('/', async function (req, res) {
      let apiMethod = await service.getTransactionResult(txHash).execute();

      async.retry({times: 60, interval: 1000}, apiMethod, function(err, result) {
           if(err){
              console.log(err);
           }else{
              return result;
           }
           
      });
});

module.exports = router;

How ever I can't figure out the correct syntax with the apiMethod's await function.

I'm getting the error Error: Error: Invalid arguments for async.retry

How do I implement it so the errors are all logged everytime it fails and also how to successfully exit the 60 retry loop if it succeeds before all 60 are finished? (if it finishes at for example 14, stop the retries).

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

0

You could try the async-retry library.

await retry(
  async (bail) => {
    const res = await service.getTransactionResult(txHash).execute();
    if (res.status >= 400) {
      bail(new Error());
      return;
    }

    const data = await res.text();
    return data;
  },
  {
    retries: 60,
  }
);
about 4 years ago · Juan Pablo Isaza Denunciar

0

As I can see it, apiMethod is supposed to be an unresolved promise, try this out

router.get('/', async function (req, res) {
      let apiMethod = () => (service.getTransactionResult(txHash).execute())

      async.retry({times: 60, interval: 1000}, apiMethod, function(err, result) {
           if(err){
              console.log(err);
           }else{
              return result;
           }
           
      });
});

module.exports = router;
about 4 years ago · Juan Pablo Isaza Denunciar

0

var promises = [];
    let request = service.getTransactionResult(txHash);
    for(let i = 0; i < 60; i++){
        promises.push(request);
    }
    for(let i = 0; i < 60; i++){
        try{
            let result = await promises[i].execute();
        }catch(err){
            console.log(err);
            await new Promise(resolve => setTimeout(resolve, 1000));
        }
        
    }

I couldn't figure out how to do it with the async library but this worked for polling in my situation where the error would be thrown and waits a second when the transaction hasn't been processed yet and then retried.

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