Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!