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.
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.
A quick 3-question quiz on this lesson — instant feedback.
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.
Project-based Node.js Foundations courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for Node.js Foundations books — check the publication year so the examples match current versions.
Search Amazon →