June 2025
Intermediate to advanced
837 pages
24h 50m
English
Subjects represent a special type of observable in RxJS. One of their most important features is that you can subscribe to a subject multiple times, whereas with the observable, you can only do that once, as shown in Listing 17.28.
import { Observable, Subject } from 'rxjs'; const observable = Observable.create((observer) => { setTimeout(() => { observer.next(Math.random()); }, 1000);}); observable.subscribe((data) => console.log('Observer 1: ', data));observable.subscribe((data) => console.log('Observer 2: ', data)); const subject = new Subject(); subject.subscribe((data) => console.log('Subject observer 1:', data));subject.subscribe((data) => console.log('Subject observer 2:', data)); subject.next(Math.random()); /* Output: ...
Read now
Unlock full access