Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

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].

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

How can I load optional data without delaying the main content? I have a main list and a secondary section of recommendations. I want the list to appear even if the recommendations are slow or unavailable.

Geez

Treat them as two jobs with separate rendering paths. The main view should depend only on its essential request. As soon as that result is ready, render it. The optional section can initially say it is loading, then show recommendations, an empty state, or a failure message.

Here’s a small example using invented local data. The render... functions stand in for your application’s UI updates:

const localItems = [
  { id: 1, name: "Notebook" },
  { id: 2, name: "Pencil" }
];
const localRecommendations = ["Eraser", "Ruler"];

const getItems = () => Promise.resolve(localItems);
const getRecommendations = () => Promise.resolve(localRecommendations);

async function loadPage() {
  renderItemsLoading();
  renderRecommendationsStatus("Loading recommendations…");

  // Handle optional success or failure independently, without waiting here.
  const recommendationsTask = Promise.resolve()
    .then(() => getRecommendations())
    .then((recommendations) => {
      if (recommendations.length === 0) {
        renderRecommendationsStatus("No recommendations to show.");
      } else {
        renderRecommendations(recommendations);
      }
    })
    .catch(() => {
      renderRecommendationsStatus(
        "Recommendations are unavailable. The main list is still ready."
      );
    });

  try {
    const items = await getItems();
    renderItems(items);
  } catch (error) {
    renderItemsError("The main list could not be loaded.");
  }
}

The optional task handles its own failure, including a synchronous error from getRecommendations(). It doesn’t hold up the await getItems() path. The example functions and render functions are placeholders; replace them with your application’s data and UI.

Follow-up question

Why not put both requests in Promise.all()? Wouldn’t that be simpler?

Geez

Promise.all() waits for all promises in its group to fulfill before its combined promise fulfills, and rejects if any promise rejects [S1]. If you render the main view only after awaiting a Promise.all() containing both requests, a slow optional request can delay that render, and an optional failure can send the combined operation to its error handling.

You can use Promise.all() when both results are required before the next step. For this task, keep the essential request as the gate for the main view and handle the optional request independently. Promise.allSettled() reports each outcome after all promises settle [S1], which can help when you want to collect results together, but it does not itself make the primary view render sooner. The timing and status display are application-level choices; the promise reference does not prescribe a UI pattern.

Follow-up question

What should the optional section show while it loads or if it fails? I don’t want a blank box to look like a bug.

Geez

Give that section its own small set of states, with wording that reflects what happened:

  1. Loading: “Loading recommendations…”
  2. Success: show the recommendations.
  3. Empty result: “No recommendations to show.”
  4. Failure: “Recommendations are unavailable. You can still use the main list.”

These messages are examples, not required wording. The useful distinction is between a successful response with no items and a request failure; don’t present one as the other. Keep the status within the optional section so it doesn’t replace or obscure the main content. If you add a retry control, make it retry only the optional request unless the main data also needs refreshing.

Follow-up question

Can you give me a reusable check before I add this pattern to a page?

Geez

Use this checklist, then check the three optional outcomes with controlled example results: success, an empty result, and failure. Also try a slow optional result while the essential result is ready; confirm the main view can render before the optional result arrives. These are implementation checks for your application, not claims about a particular framework or performance outcome.

Copyable status plan:

Main section: loading → content OR main-data error
Optional section: loading → content OR empty OR optional-data error
Optional failure: keep main content available

Keep the optional task’s error handling close to its status update. That makes it easier to see that an optional failure changes the optional section, not the essential view.

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.

  1. [S1] Promise — JavaScript reference (Microsoft Learn)
    learn.microsoft.com · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work