Promise Retry Design Patterns
There are many good solutions mentioned and now with async/await these problems can be solved without much effort.
If you don't mind a recursive approach then this is my solution.
function retry(fn, retries=3, err=null) {
if (!retries) {
return Promise.reject(err);
}
return fn().catch(err => {
return retry(fn, (retries - 1), err);
});
}
Something a bit different ...
Async retries can be achieved by building a .catch()
chain, as opposed to the more usual .then()
chain.
This approach is :
- only possible with a specified maximum number of attempts. (The chain must be of finite length),
- only advisable with a low maximum. (Promise chains consume memory roughly proportional to their length).
Otherwise, use a recursive solution.
First, a utility function to be used as a .catch()
callback.
var t = 500;
function rejectDelay(reason) {
return new Promise(function(resolve, reject) {
setTimeout(reject.bind(null, reason), t);
});
}
Now you can build .catch chains very concisely :
1. Retry until the promise resolves, with delay
var max = 5;
var p = Promise.reject();
for(var i=0; i<max; i++) {
p = p.catch(attempt).catch(rejectDelay);
}
p = p.then(processResult).catch(errorHandler);
DEMO: https://jsfiddle.net/duL0qjqe/
2. Retry until result meets some condition, without delay
var max = 5;
var p = Promise.reject();
for(var i=0; i<max; i++) {
p = p.catch(attempt).then(test);
}
p = p.then(processResult).catch(errorHandler);
DEMO: https://jsfiddle.net/duL0qjqe/1/
3. Retry until result meets some condition, with delay
Having got your mind round (1) and (2), a combined test+delay is equally trivial.
var max = 5;
var p = Promise.reject();
for(var i=0; i<max; i++) {
p = p.catch(attempt).then(test).catch(rejectDelay);
// Don't be tempted to simplify this to `p.catch(attempt).then(test, rejectDelay)`. Test failures would not be caught.
}
p = p.then(processResult).catch(errorHandler);
test()
can be synchronous or asynchronous.
It would also be trivial to add further tests. Simply sandwich a chain of thens between the two catches.
p = p.catch(attempt).then(test1).then(test2).then(test3).catch(rejectDelay);
DEMO: https://jsfiddle.net/duL0qjqe/3/
All versions are designed for attempt
to be a promise-returning async function. It could also conceivably return a value, in which case the chain would follow its success path to the next/terminal .then()
.
2. Pattern that keeps on retrying until the condition meets on the result (with delay and maxRetries)
This is an nice way to do this with native promises in a recursive way:
const wait = ms => new Promise(r => setTimeout(r, ms));
const retryOperation = (operation, delay, retries) => new Promise((resolve, reject) => {
return operation()
.then(resolve)
.catch((reason) => {
if (retries > 0) {
return wait(delay)
.then(retryOperation.bind(null, operation, delay, retries - 1))
.then(resolve)
.catch(reject);
}
return reject(reason);
});
});
This is how you call it, assuming that func
sometimes succeeds and sometimes fails, always returning a string that we can log:
retryOperation(func, 1000, 5)
.then(console.log)
.catch(console.log);
Here we're calling retryOperation asking it to retry every second and with max retries = 5.
If you want something simpler without promises, RxJs would help with that: https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/retrywhen.md