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')
}
});