
Cold vs Hot Observables in RxJS Explained
Understanding the difference between cold and hot observables is essential when working with RxJS. Depending on how values are produced, observables fall into two categories Cold and Hot . Cold Observables A cold observable produces values internally and only starts producing them when someone subscribes . This means that each time an Observer subscribes, the Observable starts a new execution and produces a fresh set of values for that specific Observer. A classic example of a cold observable is an HTTP request. In RxJS, the ajax() operator creates a cold observable: const coldObservable$ = of('Movie 1', 'Movie 2', 'Movie 3'); coldObservable$.subscribe(value => console.log('Observer 1:', value)); coldObservable$.subscribe(value => console.log('Observer 2:', value)); // Output: // Observer 1: Movie 1 // Observer 1: Movie 2 // Observer 1: Movie 3 // Observer 2: Movie 1 // Observer 2: Movie 2 // Observer 2: Movie 3 In this case Observer 1 triggers an HTTP request, Observer 2 triggers anot
Continue reading on Dev.to Tutorial
Opens in a new tab




