Home Manual Reference Source Test Repository

Function

Static Public Summary
public

Catches errors on the observable to be handled by returning a new observable or throwing an error.

public

tap(nextOrObserver: Observer | function, error: function, complete: function): Observable

Perform a side effect for every emission on the source Observable, but return an Observable that is identical to the source.

Static Public

public catchError(selector: function): Observable source

Catches errors on the observable to be handled by returning a new observable or throwing an error.

Params:

NameTypeAttributeDescription
selector function

a function that takes as arguments err, which is the error, and caught, which is the source observable, in case you'd like to "retry" that observable by returning it again. Whatever observable is returned by the selector will be used to continue the observable chain.

Return:

Observable

An observable that originates from either the source or the observable returned by the catch selector function.

Example:

Continues with a different Observable when there's an error

Observable.of(1, 2, 3, 4, 5)
  .map(n => {
	   if (n == 4) {
	     throw 'four!';
    }
   return n;
  })
  .catch(err => Observable.of('I', 'II', 'III', 'IV', 'V'))
  .subscribe(x => console.log(x));
  // 1, 2, 3, I, II, III, IV, V
Retries the caught source Observable again in case of error, similar to retry() operator

Observable.of(1, 2, 3, 4, 5)
  .map(n => {
	   if (n === 4) {
	     throw 'four!';
    }
	   return n;
  })
  .catch((err, caught) => caught)
  .take(30)
  .subscribe(x => console.log(x));
  // 1, 2, 3, 1, 2, 3, ...
Throws a new error when the source Observable throws an error

Observable.of(1, 2, 3, 4, 5)
  .map(n => {
    if (n == 4) {
      throw 'four!';
    }
    return n;
  })
  .catch(err => {
    throw 'error in source. Details: ' + err;
  })
  .subscribe(
    x => console.log(x),
    err => console.log(err)
  );
  // 1, 2, 3, error in source. Details: four!

public tap(nextOrObserver: Observer | function, error: function, complete: function): Observable source

Perform a side effect for every emission on the source Observable, but return an Observable that is identical to the source.

Intercepts each emission on the source and runs a function, but returns an output which is identical to the source as long as errors don't occur.

Returns a mirrored Observable of the source Observable, but modified so that the provided Observer is called to perform a side effect for every value, error, and completion emitted by the source. Any errors that are thrown in the aforementioned Observer or handlers are safely sent down the error path of the output Observable.

This operator is useful for debugging your Observables for the correct values or performing other side effects.

Note: this is different to a subscribe on the Observable. If the Observable returned by do is not subscribed, the side effects specified by the Observer will never happen. do therefore simply spies on existing execution, it does not trigger an execution to happen like subscribe does.

Params:

NameTypeAttributeDescription
nextOrObserver Observer | function
  • optional

A normal Observer object or a callback for next.

error function
  • optional

Callback for errors in the source.

complete function
  • optional

Callback for the completion of the source.

Return:

Observable

An Observable identical to the source, but runs the specified Observer or callback(s) for each item.

Example:

Map every click to the clientX position of that click, while also logging the click event
var clicks = Rx.Observable.fromEvent(document, 'click');
var positions = clicks
  .do(ev => console.log(ev))
  .map(ev => ev.clientX);
positions.subscribe(x => console.log(x));

See: