in my application, I have a promised function "serverGet()" which makes a server call. At times, the server returns me an HTTP 200 response but with an empty object. If I try 2 or 3 times, I get the full JSON. Time to find a final solution, I want to create a JS script allowing to re-execute this promised function a number of times until I get the result.
Here is what I developed but unfortunately, it does not return anything on retry 4-3-2-1 :
function promiseWithRetry(fn, retries){
console.log("retry :"+retries);
return new Promise(function(resolve, reject) {
fn.then(function(response){
if(_.isEmpty(response) && retries > 0){
console.log("start new retry");
Promise.resolve(promiseWithRetry(fn, retries-1));
}else{
console.log("resolve");
resolve(response);
}
});
});
}
promiseWithRetry(serverGet(parameters), 5).then(function(response){
console.log(response)
});
To simulate serverGet(), I have written in jsfiddle a small random function so execute the code several times to test it :
https://jsfiddle.net/christophes/krgvsfcy/
Can you help me ?
Christophe