What is the difference between hot and cold Observables, and what does shareReplay do?
Model answer
A cold Observable starts a new execution for each subscriber, like an HTTP call: two subscribers make two requests. A hot Observable shares one source among subscribers, like mouse events or a WebSocket.
shareReplay turns a cold stream into a shared one and replays the latest values to late subscribers, so several parts of the app can use one HTTP result. Be careful with its refCount option and cache invalidation, or you can keep stale data or leak a subscription.
Learn this topic
Angular Tutorial: Beginner to Advanced .
Sign in to add your own answer.
More questions
- How do you call an API in Angular?
- What is an Observable, and how is it different from a Promise?
- What is the difference between subscribe and the async pipe?
- What are common RxJS operators you use, and what do they do?
- Explain the difference between switchMap, mergeMap, concatMap and exhaustMap.
- What are HTTP interceptors, and what are they used for?