How can I load optional data without delaying the main content?
Quick answer
Start the essential and optional requests separately. Wait only for the essential request before rendering the main view; let the optional request update its own section when it finishes or fails. This keeps optional work from holding up the primary content and makes its status visible. Promise.all() rejects if any promise rejects, while Promise.allSettled() waits for all promises to settle [S1].
The question, the details, the next step.
A practical checklist
Identify which data is essential and which can be omitted without blocking the main view.
Start the optional request separately from the essential request.
Render the main view as soon as its essential data is ready.
Give the optional section distinct loading, success, empty, and failure states.
Check that optional failure leaves already-rendered main content available.
A few more useful answers
Does `Promise.allSettled()` make the main content render sooner?
No. It waits for every promise in its group to settle and reports each outcome [S1]. Render the essential result independently if it must appear without waiting for optional work.
Should an empty optional result be shown as an error?
No. If the request succeeds but returns no items, show an empty state. Reserve the error state for a failed request.
What should happen when the essential request fails?
Show an error for the main section because its required data is unavailable. Handle the optional section separately; its result does not change the essential request’s outcome.
Sources
References used for this guide.
- [S1] Promise — JavaScript reference (Microsoft Learn) learn.microsoft.com · Checked
