Angular Observables: Unicast, Multicast, Cold, Hot, Promises

6 important interview question for angular developers: 1. What is the difference between Unicast and Multicast Observables? - Unicast means each subscriber gets its own independent execution of the observable, so the producer runs separately for each subscriber. Multicast means multiple subscribers share the same execution and receive the same data stream. - By default, Observables are unicast. - Multicast is achieved using Subjects or operators like share(). 2. What is the difference between Cold and Hot Observables? - Cold Observables start emitting values only when subscribed, and each subscriber gets a fresh execution. - Hot Observables emit values regardless of subscriptions, and subscribers receive only ongoing values (they may miss past emissions). Example: Cold → HTTP calls Hot → DOM events, WebSocket, Subject 3. How are Unicast/Multicast related to Cold/Hot Observables? - Cold Observables are usually unicast because each subscription creates a new execution. - Hot Observables are usually multicast because the same data source is shared among multiple subscribers. However, this is not a strict rule—operators like shareReplay() can convert cold observables into multicast ones. 4. What is the difference between Promise and Observable? - A Promise emits a single value and executes immediately when created, while an Observable can emit multiple values over time and executes only when subscribed. - Observables also support cancellation using unsubscribe() and provide powerful operators like map, switchMap, etc., which Promises lack. 5. Why does Angular use Observables instead of Promises? - Angular uses Observables because they support streams of data, lazy execution, cancellation, and powerful operators for handling complex async flows like HTTP calls, user input, and real-time updates. This makes them more flexible and scalable than Promises. 6. What happens if you subscribe multiple times to an Angular HTTP Observable? - Angular HTTP Observables are cold and unicast, so each subscription triggers a separate API call. To avoid multiple calls, you can use operators like shareReplay(1) to share the response among subscribers. ------------ Read my medium article, I write about Angular and JavaScript related interview content. https://lnkd.in/gUYuypyT #angular #javascript #interview

Nice share Bittu Kumar With Analogy: 🧊 Cold Observable = Netflix Every subscriber gets their own execution. Late? No problem — starts fresh for you. 🔴 Hot Observable = YouTube Live Stream is already running. You just tune in — and yes, you miss what happened before. 📡 Unicast = Individual phone calls 1 subscriber → 1 producer. Default HTTP calls work this way. 2 components subscribing = 2 network requests. 🤦 📢 Multicast = Group call 1 producer shared across all subscribers. Efficient. That's what share() and shareReplay() do. The relationship? → Cold is naturally Unicast → Hot is naturally Multicast → shareReplay(1) converts Cold → Hot+Multicast in Angular (most used pattern)

I am getting this unicast and multicast question in an interview

An easy way to remember cold & hot observables, 1. Cold observable data is produced Inside. Dd 2. Hot observable data is produced Outside. Oo Reference luuk Grujis medium post

See more comments

To view or add a comment, sign in

Explore content categories