How can I keep one failed request from leaving a loading indicator stuck?
Quick answer
Put the loading-state reset in a finally block so it runs whether the request succeeds or rejects. Handle the error separately so the user gets useful feedback. If you offer a retry, keep the error and retry option visible, but end the loading state when that attempt has settled; start loading again when a new attempt begins.
The question, the details, the next step.
A practical checklist
Set loading to true at the start of each request attempt.
Place the awaited request inside the
tryblock.Handle the failure in
catch, or ensure a caller handles it.Reset loading in
finallyso success and rejection both reach cleanup.Show retry availability separately from whether a request is in progress.
A few more useful answers
Does `finally` turn a failed request into a successful one?
No. It runs cleanup after settlement; it does not make a rejected request succeed. Handle or propagate the rejection separately.
Can I put the error message update in `finally`?
Usually, keep outcome-specific messages in catch and shared cleanup in finally. Code that throws or rejects inside finally can alter the resulting promise outcome [S1].
Sources
References used for this guide.
- [S1] Promise.prototype.finally() — JavaScript | MDN developer.mozilla.org · Checked
