Bird
0
0

How can you modify this code to retry an async operation up to 3 times if it fails?

hard📝 Application Q9 of 15
Node.js - Error Handling Patterns
How can you modify this code to retry an async operation up to 3 times if it fails?
async function fetchData() {
  return await fetchFromAPI();
}
AWrap fetchFromAPI in a single try/catch and call it once
BUse a loop with try/catch inside to retry fetchFromAPI up to 3 times
CCall fetchFromAPI three times without error handling
DUse Promise.all to call fetchFromAPI three times simultaneously
Step-by-Step Solution
Solution:
  1. Step 1: Understand retry logic

    Retry requires repeating the async call until success or max attempts reached.
  2. Step 2: Implement loop with try/catch

    Using a loop with try/catch inside allows catching errors and retrying up to 3 times.
  3. Final Answer:

    Use a loop with try/catch inside to retry fetchFromAPI up to 3 times -> Option B
  4. Quick Check:

    Retry with loop and try/catch = B [OK]
Quick Trick: Use try/catch inside a loop to retry async calls [OK]
Common Mistakes:
  • Calling async function multiple times without catching errors
  • Using single try/catch without retry logic
  • Using Promise.all for retries incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes