BeginnerCoder

Lesson 21 — Retry wrapper

Try again before giving up

Network calls sometimes fail for a moment and then work. A retry wrapper calls a function, and if it throws, tries again — up to a maximum number of attempts. On the last failure it re-throws the error so the caller knows.

for (let n = 1; n <= limit; n++) {
  console.log("try number", n);   // the real call goes here
}

Why it matters: a few retries turn many "random" errors into invisible hiccups.

Want it another way?
Task

Finish retry(): call fn inside try; on error log "attempt i failed"; re-throw on the last attempt.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with Node.js Foundations

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.