Skip to main content
Saved
Guide implementation Intermediate 4 min read

Managing Async UX States

Patterns for handling loading and error states across your frontend.

Network requests add complications that synchronous code doesn’t have. Without careful planning, your application might flicker between states and show generic error messages that don’t help users understand what happened. This guide covers how to handle async operations so users can still trust your application when requests are slow or fail.

Plan Your Async States

Each API call goes through the same predictable set of states. In my experience, most developers think about “loading” and “success”, but they don’t think about the other states until those states cause problems in production.

  • Map the state lifecycle: For each API call, it’s worth working out what your UI does in each of six states: idle before the call starts, pending while it’s in progress, successful when it completes, empty when no data comes back, partial when some of the data comes back but not all of it, and failed when there’s an error. Your UI needs to handle all six, including the ones that seem rare.
  • Split large requests: If a large form sends everything in one request, it can be hard to work out what went wrong when that request fails. If you break it into smaller calls, you can show users exactly which section had a problem.
  • Agree on loading and retry behavior early: It helps to agree with your designers, before you start building, on when to block actions during loading and how retries should work. Those conversations are much easier to have before you’ve written any code.

Protect Your UI with Error Boundaries

A single failed API call shouldn’t break the whole page.

  • Wrap risky areas: You can wrap the sections that fetch data in Async Boundary components. If a fetch fails, the boundary catches the error and shows fallback UI, so users don’t end up looking at a blank screen.
  • Add page-level boundaries: Component boundaries work well, but it’s worth adding route-level error pages too, for serious failures. Those give you a safety net when several things go wrong at the same time.
  • Track boundary triggers: It’s worth logging each time a boundary catches an error. If one boundary catches errors often, that suggests a reliability problem in the section it wraps.

Show Clear Loading Feedback

If users don’t see any feedback quickly, they often click again, which sends duplicate requests and can cause race conditions.

  • Match loaders to context: The right Loading State pattern depends on where it appears. A small spinner works well in a button, and a skeleton screen works well for a content area.
  • Treat a refresh differently from the first load: Users are fine with a skeleton screen when the page first loads, because they’re already expecting to wait. If you’re refreshing data they’ve already seen, they’d rather see a small indicator than watch the whole UI disappear and rebuild.

Update UI Immediately with Optimistic Updates

If you wait for the server to confirm a change before you update the UI, your application can feel slow. It feels more responsive if you assume the request will succeed and deal with the failure if it happens.

  • Show changes immediately: With Optimistic Updates, the UI shows the user’s action right away, before the server confirms it. You keep a copy of the previous state so you can put it back if the server rejects the change. The application feels faster than it really is, because users don’t have to wait for the round trip to the server.
  • Prevent conflicts: While you’re waiting for the server to confirm, you can either disable the actions that would conflict or clearly mark items as “saving.” Otherwise users might repeat an action several times, which sends duplicate requests that race each other to the server.

Handle Failures Gracefully

Network requests fail regularly, for reasons such as a mobile connection dropping or a corporate firewall blocking an unfamiliar endpoint.

  • Retry with backoff: A Retry strategy with exponential backoff waits a little longer before each new attempt, instead of repeating the failed request right away. It helps to show users when you’re retrying and how many attempts are left, because if the retries are silent and take a long time, users might think the application has locked up.
  • Cancel outdated requests: With Request Cancellation, you can abort old API calls when users navigate away or change their search filters. Without it, a slow request can finish after a newer one, and the UI might show stale results.
  • Log useful failure information: A generic error message in your logs doesn’t help much when you’re debugging. It’s more useful to log which endpoint failed, how long the request took, the error code, and whether a retry succeeded, because that makes production issues much easier to diagnose.

Test and Monitor Async Behavior

Async problems often only appear in production, on real users’ networks.

  • Test failure scenarios: Your local development environment gets fast responses, so it doesn’t show you how your application behaves on a slow or unreliable network. Integration tests that simulate slow responses and server errors can check that your error boundaries and loading states work the way you intended.
  • Watch pending requests: It’s worth monitoring how many requests are in flight at once in production. If that count keeps climbing, either your cancellation logic isn’t working or your backend can’t keep up, and both of those are worth investigating.
  • Write clear error messages: A generic message doesn’t tell users what happened or what to do next. A specific one, like “We couldn’t save your changes because the server is overloaded. We’ll retry automatically in a few seconds”, is more helpful, and a UX writer can help you get the wording right.

Remember: Users run into loading states and errors all the time, and how well you handle them affects how much they trust your application.

Next Guide · 5 min read

Securing Frontend Data Flows

A defense-in-depth checklist for reducing the risk of client-side injection and protecting your users' data.

Read the next guide →

Keep Exploring

A monthly email
from Den Odell.

What I'm thinking about frontend patterns, plus news when the site changes.

No spam. Unsubscribe anytime.