I have a function which needs to check a value. If it doesn't exist then it needs to wait and then call itself again. Below is the code. But it doesn't seem to wait for 5 sec but keeps executing without waiting it seems. How do I ix it?
loadAPI(status: string) {
.....
if (this.result === "done") {
.....
}
else
{
this.sleep(5000);
loadAPI(this.status);
}
}});
}
sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
CodePudding user response:
I've edited you example code using vanilla js and async/await, you can implement it in Typescript as well
class App
{
async loadAPI(status) {
console.log(status)
if (false) {
}
else
{
await this.sleep(2000);
this.loadAPI(status);
}
}
async sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
}
(new App).loadAPI('loadAPI called')