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

140
Vistas
Mocha - what's the solution to wait for response to be as expected?

I'm using mocha to test an API and I need to wait until response is as i expect. I was struggling figuring out what's the best practice to do it, because I don't want to use setTimeout() - that would be lazy, bad practice. I want to perform an api call every few seconds until assert is fulfilled, or if not possible, a condition is true. tried to use setInterval() but it doesn't work as expected inside of it (anyone know why?). anyone has any idea how to achieve such goal?

this my code -

it.only('' , async() => {
        const userId = "test";
        const deviceId = "test";
        let counter = 0;

        const ticketId = await apiHelpers.getTicketId(deviceId, userId);
        let intervalID = setInterval(function() {
            
            response = await apiHelpers.getTicketById(ticketId);
            counter++; 
        }, 20000);

        if (response.tags.includes('enriched')) {
            clearInterval(intervalID);
        }

        else if (counter > 2) {
            clearInterval(intervalID);
            assert.fail('not enriched')
        }
   });
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