Bounded retries and failure classification
How it works
A retry invokes an operation again after a failure. Limit attempts, classify transient failures, and propagate permanent failures immediately. For operations with side effects, ensure retries are safe or use an idempotency mechanism supported by the service; a lost response does not prove the original effect failed. Backoff increases delay between attempts, often with jitter to avoid synchronized retries. Inject delay and random sources for deterministic tests; this fixture uses no real waiting.
Example 1
async function retry(task, attempts, wait) { if (!Number.isInteger(attempts) || attempts < 1) throw new RangeError("attempts"); for (let i=0;i<attempts;i++) { try { return await task(); } catch(e) { if (!e.transient || i===attempts-1) throw e; await wait(2**i); } }}let count=0; retry(async()=>{if(++count<3) throw {transient:true}; return "ok";},3,async n=>console.log("wait",n)).then(v=>console.log(v,count));Expected output in the tested environment:
wait 1wait 2ok 3The injected wait observes backoff without wall-clock dependence.
Example 2
let attempts=0; async function task(){attempts++; throw new Error("invalid");}(async()=>{try{await task();}catch(e){console.log(e.message,attempts);}})();Expected output in the tested environment:
invalid 1A permanent validation failure is propagated without retry.
Common mistake
Unlimited retries can amplify an outage. Bound delays and attempts, observe cancellation and avoid blindly retrying unsafe side effects.
Practice
Add a deterministic permanent-error case to the retry example and a case exhausting every allowed attempt. Verify wait is not called after the last failure.
Run this practice in the documented local environment. ByteMind module and asynchronous grader support is not yet verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Bounded retries and failure classification — Knowledge Check
Reason about the concept and predict its behavior in the indicated environment.
Which failure should normally be retried automatically?
What is a reason to add jitter to backoff?
Why inject the wait function in tests?
For Bounded retries and failure classification, what is the console output of example 1 in Node.js 22.16.0?
async function retry(task, attempts, wait) { if (!Number.isInteger(attempts) || attempts < 1) throw new RangeError("attempts"); for (let i=0;i<attempts;i++) { try { return await task(); } catch(e) { if (!e.transient || i===attempts-1) throw e; await wait(2**i); } }}let count=0; retry(async()=>{if(++count<3) throw {transient:true}; return "ok";},3,async n=>console.log("wait",n)).then(v=>console.log(v,count));For Bounded retries and failure classification, what is the console output of example 2 in Node.js 22.16.0?
let attempts=0; async function task(){attempts++; throw new Error("invalid");}(async()=>{try{await task();}catch(e){console.log(e.message,attempts);}})();