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 take() operator

This operator returns an observable containing a given number of contiguous data from the observable. It has the following signature:

observable.take(count,[scheduler]); 

The first parameter is mandatory and the last is optional:

  • count: This is the number of elements to be retrieved from the observable
  • scheduler: This is used to deliver the onCompleted() method in the case of the count equaling 0

The usage of this operator is really simple and can be seen in the following example:

Rx.Observable     .of(1,3,5)     .take(2)     .subscribe((i)=>console.log(i)); 

In this example, we will take only the first two elements in the sequence, so running this code, we will see the following output:

    1    3

This operation is illustrated ...

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