CrackFrontendCF
Resources
Practice
CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna

CrackFrontendCF
Resources
Practice

πŸ”„ Promise Retry Implementation

Automatically retries a failed async operation a specified number of times with optional delays. Essential for handling transient failures.

Promise retry is a resilience pattern that automatically retries a failed async operation a specified number of times before giving up. This is essential for handling transient network failures, rate limits, or intermittent service unavailability. -- βœ… Implementation -- πŸ§ͺ Example: -- βœ… Features: is retried up to times. Optional (ms) between retries. Stops on first success. Rejects with final error if all fail. -- <!-quiz-start --Q1: In the retry implementation, what happens when a retry succeeds? [ ] It continues retrying until all attempts are exhausted [x] It immediately resolves and stops further retry attempts [ ] It collects the result and waits for other retries [ ] It logs the success and retries once more to confirm Q2: Why must the first argument to retry() be a function that returns a promise, rather than a promise itself? [ ] Functions are faster than promises [ ] It's required for TypeScript compatibility [x] A promise starts executing immediately when created, so we need a function to create fresh promises for each retry [ ] Functions provide better error messages Q3: What is a common real-world use case for promise retry? [ ] Caching API responses [ ] Running promises in parallel [x] Handling transient network failures or rate limits [ ] Converting callbacks to promises <!-quiz-end --
JavaScriptPromises
🎯 Promise.all() Implementation
hard
βœ… Promise.allSettled() Implementation
medium
🎯 Promise.any() Implementation
medium
πŸ›‘ Cancelable Promise Implementation
medium
πŸ”§ Custom Promise Class Implementation
hard
🏁 Promise.race() Implementation
medium
πŸ”„ Promise Retry Implementation
easy
πŸ“‹ Sequential Promise Execution
medium
7 of 8
LibraryJavaScriptPromises46 of 61

πŸ”„ Promise Retry Implementation

jspromiseseasy

Promise retry is a resilience pattern that automatically retries a failed async operation a specified number of times before giving up. This is essential for handling transient network failures, rate limits, or intermittent service unavailability.


βœ… Implementation

function retry(fn, retries = 3, delay = 0) {
  return new Promise((resolve, reject) => {
    const attempt = (n) => {
      fn()
        .then(resolve)
        .catch((err) => {
          if (n === 0) return reject(err);
          setTimeout(() => attempt(n - 1), delay);
        });
    };
    attempt(retries);
  });
}

πŸ§ͺ Example:

let counter = 0;
const unstableTask = () => {
  return new Promise((res, rej) => {
    counter++;
    if (counter < 3) rej("fail " + counter);
    else res("success on attempt " + counter);
  });
};

retry(unstableTask, 5, 500)
  .then(console.log)
  .catch(console.error);

βœ… Features:

  • fn is retried up to retries times.
  • Optional delay (ms) between retries.
  • Stops on first success.
  • Rejects with final error if all fail.

Quick Quiz

Test your understanding with 3 quick questions

Q1In the retry implementation, what happens when a retry succeeds?
Q2Why must the first argument to retry() be a function that returns a promise, rather than a promise itself?
Q3What is a common real-world use case for promise retry?

Continue Reading

CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna