There is a scenario where I have an asynchronous method that can either return success or failure. The requirement is to repeatedly call this async method from another function until it returns success. However, if it fails consecutively for 5 times, the calling should be stopped.
let count = 0;
function myAsyncApi(url){
//This is a simulated async method that will eventually return success
return new Promise((resolve, reject) => {
if(count === 5){
setTimeout(function(){
resolve('success')
}, 100);
}
else{
setTimeout(function(){
reject('failure');
}, 100);
}
count++;
});
}
function retry(){
// The aim is to keep calling myAsyncApi('/url') from this function continuously
// Stop calling the API as soon as we receive success from myAsyncApi(url)
// If the result is a failure, repeat the call to myAsyncApi('/url') until count hits 5
// How can we implement this without utilizing async/await within this function
}