RxJS - Filtering Operator sample

This operator will give the most recent value from the source Observable, and the output will depend upon the argument passed to it emits.


sample(notifier: Observable): Observable


notifier − The argument notifier is an Observable which will decide the output to be picked.

Return value

It returns an observable, based on values emitted by the source observable.


import { fromEvent, interval } from 'rxjs';
import { sample} from 'rxjs/operators';

let btn = document.getElementById("btnclick");
let btn_clicks = fromEvent(btn, 'click');
let case1 = btn_clicks.pipe(sample(interval(4000)));
case1.subscribe(x => console.log(x));

The sample() operator is given interval(4000) so the click event will get emitted when the interval of 4seconds is done.


sample Operator
Kickstart Your Career

Get certified by completing the course

Get Started