The async pipe
Subscribing to an observable or promise directly in a template, with Angular handling the unsubscription.
The async pipe
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `| async` subscribes to an observable (or resolves a promise) directly in the template and unwraps whatever value it emits — no manual `.subscribe()` call in your component class.
- It unsubscribes automatically when the component is destroyed, which removes an entire category of subscription-leak bugs that manual subscriptions are prone to.
- Pair it with `@if`/`@for` using the `as` syntax to subscribe exactly once and reuse the resolved value across a block, instead of piping the same observable through `async` multiple times in one template.
Subscribing is a chore you shouldn’t have to repeat by hand
An observable doesn’t do anything until something subscribes to it. The
straightforward way to get a value from an observable into a template is to
subscribe in the component class, assign the emitted value to a plain property,
and bind that property in the template — which works, but means every single
observable-backed value in your component needs its own subscribe() call, its
own property to hold the latest value, and — critically — its own cleanup in
ngOnDestroy so you don’t leak that subscription when the component goes away.
That’s a lot of repeated bookkeeping for “show this value, once it exists.”
The async pipe exists to collapse all of that into the template itself.
What | async actually does
<p>{{ user$ | async }}</p>
<!-- with an object, you'll usually want to unwrap specific fields -->
<div *ngIf="user$ | async as user">
<h2>{{ user.name }}</h2>
<p>{{ user.email }}</p>
</div>
user$ | async subscribes to user$ the first time Angular evaluates that
expression, and from then on, every emission the observable produces gets handed
back to the template in place of the pipe expression. Before the first value
arrives, it’s null — which is why you’ll often see it paired with ?. or an
@if/*ngIf guard rather than assumed to always have a value immediately. And
when the component is destroyed, the async pipe unsubscribes on its own. You
never write ngOnDestroy for this subscription, because there’s no manual
subscription for you to have forgotten to clean up in the first place — the pipe
owns that lifecycle entirely.
This is the same shape of problem ngOnDestroy existed to solve a few lessons
back, solved here by removing the need for a manual subscription at all rather
than by remembering to tear one down.
Pairing it with control flow, and subscribing only once
A genuine trap: pipe the same observable through | async more than once in a
template, and you may be subscribing to it more than once —
<!-- risky: two separate subscriptions to user$ -->
<h2>{{ (user$ | async)?.name }}</h2>
<p>{{ (user$ | async)?.email }}</p>
If user$ is a cold observable wrapping, say, an HTTP request, two subscriptions
can mean two requests fired. The fix is to subscribe exactly once and hold onto
the resolved value for the rest of that block, using the as syntax:
@if (user$ | async; as user) {
<h2>{{ user.name }}</h2>
<p>{{ user.email }}</p>
}
One subscription, one resolved user, reused everywhere inside the block. This
pairs naturally with the @if/@for blocks from earlier in this roadmap —
as works the same way there as it does with the older *ngIf as syntax, just
inside the newer block form.
Where this fits going forward
The async pipe is the reason Angular code written with RxJS often has far fewer
manual .subscribe() calls than you’d expect — the next section of this roadmap
covers observables and RxJS properly, and the async pipe is the default way
almost everything you build there actually reaches a template. This lesson’s
video focuses specifically on the advantages it gives you over manual
subscription; the linked “deeper” video covers the same ground from Angular
University’s angle, useful as a second pass once the mechanics above have
settled in.
Check yourself
If `user$` is an `Observable<User>`, what does `{{ (user$ | async)?.name }}` actually do, step by step?
The `async` pipe subscribes to `user$`, and on each emission, hands the emitted `User` value back to the template in place of `user$ | async` — before the first emission arrives, that expression is `null`, which is exactly why the `?.` safe-navigation operator is there, to avoid erroring on `name` before a value exists.
Why does piping the same observable through `| async` twice in one template (`user$ | async` used in two separate places) risk a subtle bug, and what's the fix?
Each `| async` usage subscribes independently, so with a cold, non-shared observable you can end up triggering the underlying work (an HTTP request, for instance) twice. The fix is to subscribe once — typically with `@if (user$ | async; as user)` — and reuse the resolved `user` variable everywhere else in that block.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.