Set a deadline and a safe retry policy
Keep a slow dependency from leaving the interface stuck.
Define the time budget
A request can hang long enough to make the UI unusable. Set a timeout using a cancellation signal and report a clear, retryable error. The timeout should reflect the user's task rather than an arbitrary long default. Clear timers during cleanup to avoid keeping resources alive.
Retry only safe operations
A read-only GET can often be retried after a temporary network failure. A payment or application submission may create an effect before the response is lost; retry only with a server-supported idempotency key. Use a bounded number of attempts and delays so the client does not hammer a recovering service.
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 5000);
try { return await fetch(url, { signal: controller.signal }); }
finally { clearTimeout(timer); }
Show a useful state
Distinguish timeout, validation error, and an empty result. Preserve user input so retry does not require retyping. Test a slow response, a 503, and a request that succeeded on the server but lost its response; the last case is where a write needs idempotency.
Check your understanding
Why is a blind retry of a POST risky after a timeout?
Show explanation
The server may have completed the first write even though the response was lost. Use an idempotency key or reconcile before retrying.
You can read for free. Create an account to save your progress.
← Previous lesson: Own the lifecycle of a fetch requestPut this lesson into practice
A practice activity at this level in the same module.
Try: Cancel outdated search requestsFurther reading
Primary references for this module. Check the current version when behavior or framework guidance may have changed.