Introduction
When asynchronous operations fail, retrying can be an effective strategy to handle transient errors. This article presents various patterns for retrying Promise-based operations, focusing on three common scenarios.
Pattern #1: Retry Until Promise Resolves
This pattern continuously retries a promise until it resolves successfully. It specifies a delay between retries and a maximum number of attempts.
Promise.retry = function(fn, times, delay) {
return new Promise(function(resolve, reject){
var error;
var attempt = function() {
if (times == 0) {
reject(error);
} else {
fn().then(resolve)
.catch(function(e){
times--;
error = e;
setTimeout(function(){attempt()}, delay);
});
}
};
attempt();
});
};
Pattern #2: Retry Until Condition Meets
This pattern retries until a condition is met on the result of the promise. It specifies a delay between retries and a maximum number of attempts.
work.publish()
.then(function(result){
return new Promise(function(resolve, reject){
var intervalId = setInterval(function(){
work.requestStatus(result).then(function(result2){
switch(result2.status) {
case "progress": break; //do nothing
case "success": clearInterval(intervalId); resolve(result2); break;
case "failure": clearInterval(intervalId); reject(result2); break;
}
}).catch(function(error){clearInterval(intervalId); reject(error)});
}, 1000);
});
})
.then(function(){console.log('done')})
.catch(console.error);
Pattern #3: Unlimited Retry with Condition
This pattern provides a memory-efficient way to retry unlimited times. It only specifies a delay between retries.
Alternative Approach Using .catch() Chains
Unlike the patterns above, this approach builds a .catch() chain, not a .then() chain. It limits the number of attempts and is suitable for low-maximum scenarios to avoid excessive memory consumption.
function rejectDelay(reason) {
return new Promise(function(resolve, reject) {
setTimeout(reject.bind(null, reason), t);
});
}
Retry Until Resolves, with Delay
var max = 5;
var p = Promise.reject();
for(var i=0; iRetry Until Condition Meets, No Delay
var max = 5;
var p = Promise.reject();
for(var i=0; iRetry Until Condition Meets, with Delay
var max = 5;
var p = Promise.reject();
for(var i=0; iConclusion
These patterns provide versatile ways to implement retry logic for asynchronous operations. Whether using .catch() or .then() chains depends on the requirements, such as memory usage and maximum number of attempts.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3