Home > Web Front-end > JS Tutorial > body text

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

Susan Sarandon
Release: 2024-10-22 22:57:29
Original
478 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!