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

The takeLast()  operator is analogous to the take() operator. The only difference is it takes a given count from the end of the observable. It has the following signature:

observable.take(count); 

The first parameter is mandatory:

  • count: This is the number of elements to be retrieved from the end of the observable

We can change the example from the previous section to use this operator:

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

In this code, the takeLast() operator generates a new Observable containing the last two elements in the original Observable (3 and 5), so, if you run this code, you will see the following output:

    3    5

This operation is illustrated in the following diagram: ...

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