Interop between signals and observables

Intermediate · 7 min

toSignal() and toObservable() bridge Angular's two reactive systems — and when to reach for which one in the first place.

Before thisSubscription management and takeUntilDestroyedSignals, computed values, and effects

Deborah Kurata · 7 min

Interop between signals and observables

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Deborah KurataReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `toSignal()` subscribes to an observable and exposes its latest emission as a signal — the subscription's lifetime is tied to the injection context automatically, same as takeUntilDestroyed.
  • `toObservable()` goes the other direction — it turns a signal into an observable stream, so you can run RxJS operators against something that started life as reactive state.
  • The real decision isn't "which API is newer" — it's where a given piece of state naturally lives: synchronous, always-available values fit signals; asynchronous streams of events over time fit observables.

Two reactive systems that need a bridge

This roadmap now has two distinct ways of modeling something that changes over time: signals, synchronous and read with a function call, and observables, asynchronous streams you subscribe to. Neither replaced the other — Angular’s own HttpClient and Router are still observable-based, while signals have become the default for component-local state. Real code ends up needing both at once, which means something has to bridge them.

toSignal(): reading an observable’s latest value as a signal

user = toSignal(this.http.get<User>('/api/me'), { initialValue: null });
@if (user(); as u) {
  <p>{{ u.name }}</p>
}

toSignal() subscribes to the observable you give it and exposes its most recent emission as a signal — read with user(), exactly like any other signal, composable with computed() and usable directly in templates without an async pipe. The subscription it creates is tied to the current injection context automatically, the same mechanism takeUntilDestroyed() relies on — you don’t separately manage its cleanup. initialValue fills in what the signal should read as before the observable’s first emission arrives; omit it and the signal starts as undefined.

This is often a cleaner way to get an HTTP response into a template than threading an observable through the async pipe, specifically because the result composes with the rest of your signal-based state without any special-casing.

toObservable(): going the other direction

searchTerm = signal('');

results$ = toObservable(this.searchTerm).pipe(
  debounceTime(300),
  switchMap((term) => this.api.search(term)),
);

Signals have no .pipe() of their own — they’re not observables, and RxJS operators don’t run against them directly. toObservable() is the conversion that makes that possible: it emits the signal’s current value immediately, then a new value every time the signal changes afterward, as an ordinary observable you can pipe through the exact same debounceTime/switchMap typeahead pattern from several lessons ago. This is the practical case where you’d reach for it — state that naturally lives as a signal (a form field, a UI toggle) that suddenly needs RxJS’s time-based operators applied to its changes.

Picking the right tool isn’t about which API is newer

It’s tempting to treat signals as simply “the new way” and observables as “the old way,” but that framing misses what each is actually suited for. A value that’s always synchronously available right now — a count, a toggle, a computed total — fits a signal naturally; there’s no meaningful “stream of events over time” story for it. An asynchronous sequence of things arriving over time — HTTP responses, WebSocket messages, debounced user input before it’s been resolved into a final value — fits an observable’s operators far more naturally than it would fit a signal with manual, ad-hoc timing logic bolted on. toSignal() and toObservable() exist so you don’t have to pick one system and force everything into it; you pick whichever models a given piece of state most directly, and convert at the boundary where the two need to talk.

This lesson’s video, from an Angular specialist who’s covered this interop directly, walks through toSignal(); the linked “deeper” video focuses on toObservable()’s specific timing behavior — worth watching closely, since it covers a subtlety (the conversion runs inside an effect() under the hood, which has real implications for exactly when values show up) this lesson’s overview doesn’t go into.

Check yourself

  1. 01What does `toSignal(this.http.get<User>('/api/me'))` give you, and what value does it hold before the HTTP response arrives?

    A signal whose value is the latest emission from that HTTP observable — `undefined` until the response arrives, unless you supply an `initialValue` option, after which calling the signal returns the resolved `User`. It subscribes for you automatically and ties that subscription's cleanup to the current injection context, the same mechanism `takeUntilDestroyed()` uses.

  2. 02You have a signal-based form field, `searchTerm = signal('')`, and you want to run `debounceTime` and `switchMap` against its changes. What do you need, and why can't you pipe a signal directly?

    `toObservable(this.searchTerm)` — signals aren't observables and have no `.pipe()` method of their own, so converting to an observable first is required before any RxJS operator can run against the stream of its changes. Once converted, the usual debounce/switchMap typeahead pattern from earlier applies exactly as it would to any other observable.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark