Full answer
Pure pipes are re-evaluated only when their input changes. A pure pipe with the same input always returns the same output, so Angular can cache it. The async pipe cannot follow this rule. Its input (the Observable) stays the same while the values it produces change over time. So AsyncPipe is declared with pure: false, which means Angular calls transform on every change detection pass. This is cheap because it just returns the stored latest value.
What the pipe does internally:
- On the first call it subscribes to the source and returns
null(no value yet). - When a value arrives, it stores it and calls
markForCheck()on the view, so OnPush components update too. - If the input reference changes to a different Observable, it unsubscribes from the old one and subscribes to the new one.
- On destroy it unsubscribes. This is why it prevents leaks.
Common mistakes:
- Creating a new Observable in the template on each check, such as
service.load() | async. Each pass gets a new Observable, so the pipe resubscribes and fires a request every time. Create the Observable once, in a field. - Using several
asyncpipes on the same cold Observable. Each one subscribes, so an HTTP request is sent several times. Use@if (data$ | async; as data),@let data = data$ | async;, orshareReplay. - Forgetting that the first value is
null. Guard with@ifor provide a loading state. - Expecting error handling. If the Observable errors, the pipe throws. Add
catchErrorupstream.
When to prefer toSignal: it gives the same automatic cleanup and a value you can use in code and computed, and it needs no null check if you pass initialValue. The async pipe remains excellent for simple template-only cases.
Custom pipes should stay pure. Make a pipe impure only when the output depends on something other than its inputs, and then keep transform very cheap.
import { AsyncPipe } from '@angular/common';
import { Component } from '@angular/core';
import { Observable, defer, of, shareReplay } from 'rxjs';
import { delay } from 'rxjs/operators';
interface Course {
id: number;
title: string;
}
@Component({
selector: 'app-course-summary',
imports: [AsyncPipe],
template: `
<!-- One field, one shared run, several readers. -->
@let list = courses$ | async;
@if (list) {
<p>{{ list.length }} courses</p>
<ul>
@for (c of list; track c.id) {
<li>{{ c.title }}</li>
}
</ul>
} @else {
<p>Loading...</p>
}
<p>Requests sent: {{ loadCount }}</p>
`,
})
export class CourseSummaryComponent {
loadCount = 0;
// Created once. shareReplay makes later subscribers reuse the same result.
readonly courses$: Observable<Course[]> = defer(() => {
this.loadCount++;
return of([{ id: 1, title: 'Angular' }, { id: 2, title: 'RxJS' }]).pipe(delay(200));
}).pipe(shareReplay({ bufferSize: 1, refCount: true }));
// Wrong: a new Observable on every change detection pass.
// loadCourses() { return this.http.get('/api/courses'); }
// Template: loadCourses() | async
}Angular 20.3, strict template check passes. The `loadCount` field shows that the request function runs once, not on every check, because the Observable is created in a field.
Key points
- `async` is impure because its value changes over time while its input stays the same.
- It subscribes, stores the latest value, calls `markForCheck`, and unsubscribes on destroy.
- Create the Observable once. Several `async` pipes on a cold source mean several subscriptions.
- The first emission is `null`, so handle loading. `toSignal` is the alternative.
Common mistakes
- Writing `getCourses() | async` where the method returns a new Observable every time.
- Using `| async` three times on an HTTP Observable and sending three requests.
- Making a custom pipe impure to hide a stale view caused by mutation.
Follow-ups
- How does the async pipe update an OnPush component?
- How can you share one subscription between several async pipes?
- When would you choose `toSignal` over the `async` pipe?
Tips
- Explain the reason for impurity in one sentence: input does not change, output does.
- Describe the method-call pitfall. It is the most common real bug.
- Mention `@let` and `shareReplay` as fixes for double subscriptions.
Evaluation rubric
- Explains pure versus impure correctly and why async is impure.
- Knows subscription and cleanup behaviour.
- Identifies the double subscription and new-Observable pitfalls.