I'm using promises (with await). I have an async function that has to await an async request: an http request for example. The HTTP request can fail (timeout or other motivations) but I need to recall it until success or until a max number of attempts are done (let's say n attempts) and then continue the execution of the function. I wasn't able to find a clean and well organized way to do this. Below a pseudocode:
async function func(){
//DO something before HTTP request
try{
let res = await http_request();
} catch(e){
//http request failed
//WHAT TO DO HERE TO CALL AGAIN THE HTTP REQUEST until success??
//or until max attempts == n?
}
//DO other stuff only after the http request succeeded
return;
}
the idea would be to return at the end a promise which resolves if the http requests and the rest of the code succeeded or rejects if the http request attempts failed n times or other errors.
PS: the http request is an example but http_request() can be substituted with any other async function.