Angular Observables: Hot vs Cold Explained

#Angular interview question- What is the difference between Hot vs Cold Observables in Angular. - Cold Observable Every subscription creates a new execution. Example: this.http.get('/api/users').subscribe(); this.http.get('/api/users').subscribe(); Two subscriptions = Two HTTP requests. So Angular HttpClient is a Cold Observable. - Hot Observable Data stream is shared among subscribers. Example using BehaviorSubject: private userSubject = new BehaviorSubject(null); user$ = this.userSubject.asObservable(); Multiple components can subscribe to "user$", and they all receive the same shared data stream. How do you usually explain Hot vs Cold Observables in interviews? ---------- Read my medium article, i write about interview topics on angular and JavaScript. https://lnkd.in/ggmhXdvA #angular #interview #observable

A hot observable is like the radio. When you turn it on, the song is already playing, because the stream is already going, and you're just tapping in. A cold observable is like Spotify. The song starts when you connect, because the stream is just for you.

To view or add a comment, sign in

Explore content categories