O'Reilly logo

Mastering Reactive JavaScript by Erich de Souza Oliveira

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The sample() operator

The sample() operator can also be used to mitigate the problem of backpressure in a lossy way. It lets you receive data from the observable at every given interval.

The sample() operator has the following signature:

observable.sample(interval,[scheduler]); 

The first parameter is mandatory and the second is optional:

  • interval: This is the time interval, in milliseconds, between the emission of data. This parameter can also be an observable. In this case, this observable will be used to sample the data.
  • scheduler: This is used to propagate the data

Let's use the same example from the throttle() method (using the interval() method to create an Observable) and change it to use the sample() method:

Rx.Observable  .interval(50) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required