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

The first() operator returns a new observable containing only the first element in an observable which satisfies a given condition. This operator has the following signature:

observable.first([condition],[context],[defaultValue]); 

All parameters are optional:

  • condition: This is the function which the value must satisfy to be retrieved
  • context: This is an argument used in the given conditional function
  • defaultValue: This is a value used as the default if the observable is exhausted and no item satisfies the given condition

If no condition is supplied, this operator returns the first element in the Observable. This is shown in the following example:

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

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