I have a block of code that I run and I want to loop every 2 seconds until the results (val1, val2, val3) are no longer null. How can I change and wrap this to do that?
// get the data
let theData = null;
try {
theData = await gRating.loadData(request_code)
} catch (ex) {
console.error(ex);
respondWithError(res, 'API error', 500);
return;
}
the loadData function returns an array and stores it in theData that looks like this:
return [this.val1, this.val2, this.val3]
CodePudding user response:
you can use setInterval
and check if the theData
is not nullish value, then clearInterval
let theData = null;
let timeout = setInterval(async () => {
try {
theData = await gRating.loadData(request_code);
if (theData) {
clearInterval(timeout);
}
} catch (ex) {
console.error(ex);
respondWithError(res, "API error", 500);
}
}, 2000);