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

This operator is simple and useful, and works similarly to the finally from the try/catch/finally block. It executes a specified function after the original observable finishes (even in the case of failure).

It has the following signature:

observable.finally(action); 

It receives only one parameter and it is mandatory:

  • action: It is the function to be executed when the original observable finishes, even in the case of failure

This operator is simple and two examples are enough to understand it.

The first example shows its usage in an observable finishing gracefully:

Rx.Observable   .just(1)   .finally(()=>console.log('FINALLY'))   .subscribe((i)=>console.log(i)); 

If you run this code you will see the following output, ...

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