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 }}- ✓Correct answer: HTTP Observables are cold - each
asyncsubscribes separately; share withshareReplay(1)or use one subscription - 2The
asyncpipe always sends requests twice in dev mode - 3
HttpClientretries every request automatically
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 }).