Skip to content
tezvyn:

Angular's `async` Pipe: Manage Observables in Templates

Source: angular.devMediumHow cards are made

Angular's `async` Pipe: Manage Observables in Templates

Angular's async pipe is a self-managing subscription for your templates. It unwraps values from Observables or Promises directly in your HTML, triggering updates on new emissions. It automatically unsubscribes, preventing the common footgun of memory leaks.

Why it exists

Managing asynchronous data in a declarative template is tricky. Without the async pipe, you must manually subscribe to observables in your component's code, store the emitted value in a property, and remember to unsubscribe to prevent memory leaks. This adds boilerplate code and significant risk of bugs.

The mental model

The async pipe is a declarative subscription manager for your view. Instead of telling your component how to get and clean up data (imperative), you tell your template what data stream to display (declarative). The pipe handles the implementation details of subscribing, updating, and unsubscribing for you.

How it works

When you apply | async to an Observable or Promise in your template, the pipe subscribes to it. Each time the stream emits a new value, the pipe receives it and marks your component to be checked for changes, causing the view to update. When the component is destroyed, the pipe's ngOnDestroy lifecycle hook is called, and it automatically unsubscribes from the stream. If the observable reference itself changes, it cleans up the old subscription and starts a new one.

When to use it

Use it whenever you need to display data from an Observable or Promise in your template. This is the standard practice for data from HTTP requests (HttpClient), reactive forms (valueChanges), or state management libraries like NgRx. It keeps your component logic clean and simple.

When not to use it

Avoid using it if an emitted value needs to trigger complex side effects within your component's TypeScript code. In that case, subscribing manually can be clearer. Also, if you need the same value in multiple places, use the *ngIf="myObservable$ | async as myValue" syntax to create a single subscription and reuse the unwrapped value within that template block.

One canonical example

A component fetches user data from an API, which returns an Observable<User>. Instead of subscribing in ngOnInit, you assign the observable directly to a property: user = this.userService.getUser(1);. In the template, you display the user's name with {{ (user | async)?.name }}. The pipe handles the subscription, waits for the data, displays the name, and cleans up automatically. The ? provides safe navigation since the initial value is null.

Interview question

Under which circumstance would it be more appropriate to manually subscribe to an Observable in your component's TypeScript code rather than using the async pipe?

  • a.When the emitted value needs to trigger complex logic or side effects within the component's class.Correct
  • b.When the Observable is sourced from an Angular HttpClient request.
  • c.When the Observable is expected to emit values only once.
  • d.When the component displaying the data is frequently re-rendered.
Why?

The async pipe is ideal for declarative display of data. However, if an Observable's emitted value requires complex processing or side effects in TypeScript, manual subscription provides clearer control. Using it for HttpClient requests is a primary use case for the async pipe, not a reason to avoid it.

Just read this? Test yourself on what you have been reading.

Read the original → angular.dev

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on angular — each one lists the topics its interview covers.

See open roles