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

Which Design Patterns Exist for Asynchronous Promise Retries?

Mary-Kate Olsen
Release: 2024-10-22 18:53:03
Original
275 people have browsed it

Which Design Patterns Exist for Asynchronous Promise Retries?

Designing Patterns for Promise Retries

In asynchronous programming, it's often useful to retry failed promises until they resolve. Here are three design patterns for achieving this with Promises:

1. Retry Until Promise Resolves (with Delay and Max Retries)

This pattern allows you to retry a promise until it resolves successfully, with a delay between each retry. It uses a for loop to create a chain of .catch() handlers, followed by a .then() handler for the successful resolution.

<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

2. Retry Until Condition Meets on Result (without Delay)

This pattern allows you to retry a promise until a specific condition is met on the result. It uses a similar .catch() chain approach as the previous pattern, but instead of a rejectDelay, it uses a .then() handler to test the result.

<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

3. Retry Until Condition Meets on Result (with Delay)

This pattern combines the previous two patterns, allowing you to retry a promise until a condition is met, with a delay between each retry. It uses a .catch() chain to handle retries and a .then() handler to perform the test and introduce the 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

These patterns provide a concise and efficient way to handle retries with Promises, ensuring that your code continues to execute even when failures occur. They can be customized to meet the specific needs of your application, balancing factors such as maximum retries, delay intervals, and retry conditions.

The above is the detailed content of Which Design Patterns Exist for Asynchronous Promise Retries?. 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!