I am using promises to retry API requests if they fail. I am using it like so:
var promise = new Promise((resolve, reject) => {
client.ApiRequest(params, function(data) {
if (!Array.isArray(data)) {
console.log("There was an error with the request.");
reject("Rate Limit Exceeded");
return;
}
...
resolve();
});
});
Utils.retry(promise, 1500);
And then the Utils.retry method is as follows:
static delay(delayMS) {
return new Promise(function(resolve, reject){
setTimeout(resolve, delayMS);
});
}
static retry(operation, delayMS) {
return operation.catch((err) => {
console.log(err);
return Utils.delay(delayMS).then(function(){
return Utils.retry(operation, delayMS)
});
});
}
As you can see, it catches the reject()
from the promise and uses delay()
to wait before retrying. The code runs, but it seems like once the promise fails once it is constantly stuck in the rejected state.
For instance, when it fails, it delays, and then it retries the promise just to output the same reject message. However, in the first function, it does not print out the "There was an error with the request" message on subsequent retries, so I don't think it's even retrying.
Is there a way to reset this promise state so that it when it retries it executes all of the code in the promise function?