Home >Web Front-end >JS Tutorial >How to Implement Promise Retry Design Patterns for Transient Error Handling?

How to Implement Promise Retry Design Patterns for Transient Error Handling?

Susan Sarandon
Susan SarandonOriginal
2024-10-22 22:57:29541browse

How to Implement Promise Retry Design Patterns for Transient Error Handling?

Promise Retry Design Patterns

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.

<code class="javascript">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();
    });
};</code>

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.

<code class="javascript">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);</code>

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.

<code class="javascript">function rejectDelay(reason) {
    return new Promise(function(resolve, reject) {
        setTimeout(reject.bind(null, reason), t); 
    });
}</code>

Retry Until Resolves, with Delay

<code class="javascript">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);</code>

Retry Until Condition Meets, No Delay

<code class="javascript">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);</code>

Retry Until Condition Meets, with Delay

<code class="javascript">var max = 5;
var p = Promise.reject();

for(var i=0; i<max; i++) {
    p = p.catch(attempt).then(test).catch(rejectDelay);
}

p = p.then(processResult).catch(errorHandler);</code>

Conclusion

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.

The above is the detailed content of How to Implement Promise Retry Design Patterns for Transient Error Handling?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn