Angular : how to call finally() with RXJS 6

  • use throwError instead of Observable.throw, see https://github.com/ReactiveX/rxjs/blob/master/docs_app/content/guide/v6/migration.md#observable-classes

  • finally was renamed to finalize and you'll use it inside pipe() among other operators.

  • the same with publish() and refCount(). Both are operators you'll use inside pipe().


According to official document, You should change your code like this to avoid compile error: (You must throw exception in catchError method. finalize callback method has no argument.)

import { catchError, finalize } from 'rxjs/operators';

return next.handle(clonedreq).pipe(
  catchError(error => {
    console.log('error occured:', error);
    throw error;
  }),
  finalize(() => {
    console.log('finalize')
  })
);

It is successfully compiled in Angular CLI: 7.1.4.


Need to import finalize from rxjs/operators.

import { finalize } from 'rxjs/operators';

Then finalize is used inside the pipe(),

observable()
    .pipe( 
         finalize(() => {
              // Your code Here
         })
     )
    .subscribe();