Chapter 8: HTTP & Async Operations

HTTP & Async Operations!

This is the chapter where your app stops being static and starts talking to real backends. We’ll fetch data from APIs, handle loading/error states elegantly, and keep everything reactive with signals — no more async pipe in most cases, no subscription management hell.

In 2026 (Angular 19–21+), the modern patterns have evolved a lot:

  • Classic: HttpClient + toSignal() (still very useful)
  • New experimental/stabilized APIs: resource(), rxResource(), and especially httpResource() (the most convenient for pure HTTP use cases)

httpResource() is now the recommended way for most HTTP fetching in signal-based apps — it’s built on top of HttpClient, handles cancellation, loading/error states as signals, and re-fetches automatically when inputs change.

Note: As of early 2026, resource() / rxResource() / httpResource() are stabilized or very close (moved from experimental in v19 → production-ready patterns in v20+). The Angular docs push httpResource for HTTP.

We’ll cover all three approaches with examples so you see the progression.

1. Classic: HttpClient + toSignal() (Still great baseline)

First, make sure HttpClientModule (or provideHttpClient()) is set up in your app.config.ts / main.ts:

TypeScript

Example: Fetch a list of users from JSONPlaceholder (fake API)

TypeScript

→ This works, but you handle loading/error manually — not ideal.

Better: Use HttpClient with { observe: ‘response’ } or catchError, but still requires boilerplate.

2. Modern: httpResource() — The Recommended 2026 Way for HTTP

httpResource() is a reactive wrapper around HttpClient:

  • Returns signals for value, isLoading, error, status
  • Auto-cancels previous requests on new trigger
  • Re-fetches when request signal changes
  • Integrates perfectly with @if, computed, etc.
TypeScript

Key signals from httpResource ref:

  • .value() — the data (or undefined)
  • .isLoading() / .hasValue() / .hasError()
  • .error() — HttpErrorResponse or custom
  • .status — ‘idle’ | ‘loading’ | ‘success’ | ‘error’
  • .reload() — force refetch

Reactive / dependent fetch (re-fetches when signal changes)

TypeScript

→ Change userId.set(5) → auto fetch /users/5, cancels old request if pending

3. rxResource() — When You Already Have RxJS / Observables

Use this when your data comes from an RxJS stream (debounce, switchMap, combineLatest, etc.)

TypeScript

→ Great bridge when you need RxJS operators but want signal output

4. resource() — Promise-based (fetch or custom async)

For non-HTTP (e.g. IndexedDB, WebSocket first message, Promise.all)

TypeScript

But for HTTP → prefer httpResource()

5. Quick Comparison Table (2026 teacher view)

Approach Best For Loading/Error Built-in? Auto-cancel? Re-fetch on signal change? RxJS needed?
HttpClient + toSignal Simple cases, legacy code Manual Manual Manual Yes
httpResource() Most HTTP fetches (recommended) Yes (signals) Yes Yes No
rxResource() Complex streams (debounce, mergeMap) Yes Yes Yes Yes
resource() Promise-based async (fetch, etc.) Yes No (manual) Yes No

6. Mini Practice Task (try now!)

  1. Add provideHttpClient(withFetch()) if not already
  2. Create a PostDetailComponent with route /post/:id
  3. Use httpResource to fetch https://jsonplaceholder.typicode.com/posts/${id}
  4. Show title, body, loading spinner, error + retry button
  5. Bonus: Make id come from ActivatedRoute params via toSignal → auto-refetch on navigation

Template snippet:

HTML

You’ve now got modern, reactive data loading — clean templates, automatic states, cancellation.

Next is usually Forms (template-driven + reactive, with signals integration). Ready, or want to deepen HTTP (interceptors with signals, caching, polling, optimistic updates, error handling patterns)? Just tell me — we’re almost at full modern Angular apps! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *