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

This is the last operator from the take() family we will see in this chapter. This operator lets you create an observable from the first contiguous sequence of elements in an observable satisfying a given conditional function. It has the following signature:

observable.takeWhile(condition,[context]); 

The first parameter is mandatory and the last is optional:

  • condition: This is a given function which the elements must satisfy
  • context: This argument is used in the conditional function

We can use this operator to take the first contiguous sequence of even numbers, as can be seen in the following example:

Rx.Observable     .of(2,4,5,6)     .takeWhile((i)=>i%2===0)     .subscribe((i)=>console.log(i)); 

In this code, we will ...

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