Learn RxJS
Search…
ReplaySubject

"Replays" or emits old values to new subscribers

Examples

Example 1: simple ReplaySubject

1
// RxJS v6+
2
import { ReplaySubject } from 'rxjs';
3
4
const sub = new ReplaySubject(3);
5
6
sub.next(1);
7
sub.next(2);
8
sub.subscribe(console.log); // OUTPUT => 1,2
9
sub.next(3); // OUTPUT => 3
10
sub.next(4); // OUTPUT => 4
11
sub.subscribe(console.log); // OUTPUT => 2,3,4 (log of last 3 values from new subscriber)
12
sub.next(5); // OUTPUT => 5,5 (log from both subscribers)
Copied!

Additional Resources

Last modified 7mo ago