Héctor
Héctor

Reputation: 26034

Catch error in RxJS and NodeJS

I have been using package rx in NodeJS and everything is ok. Now, I tried to use rxjs (newer version of rx), and I don't understand anything.

When my Observable fails, I want to transform it in another one. Tipically, I would do it with catch, but it doesn't work anymore.

//I know it will never fail but it's just for the example
Rx.of(4).catch(err => Rx.of(7));

But I get:

Rx.of(...).catch is not a function

Same with onErrorResumeNext

Rx.of(4).onErrorResumeNext(Rx.of(7));

Rx.of(...).onErrorResumeNext is not a function

What am I doing wrong?

Upvotes: 0

Views: 554

Answers (1)

Ritwick Dey
Ritwick Dey

Reputation: 19002

catch is renamed with catchError from RxJs 6.0.

They introduced a new operator called pipe where you can add infinite number of chained operation including error catching.

import { catchError } from 'rxjs/operators';

Rx.of(4)
  .pipe(
    catchError(err => Rx.of(7))
   )

Reference : https://www.learnrxjs.io/operators/error_handling/catch.html

Upvotes: 1

Related Questions