Angular · Middle · Question 19 of 75

Why does this code send two HTTP requests, and how do you fix it?

user$ = this.http.get<User>("/api/me");
// template:
// {{ (user$ | async)?.name }}  {{ (user$ | async)?.email }}

Explanation

Every subscription runs the request. Use @if (user$ | async; as user) once, convert to a signal with toSignal, or share the stream with shareReplay({ bufferSize: 1, refCount: true }).

Further reading

Problem with a question

Angular · Middle · question 19

Test yourself

Take the Angular · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz