Function
Static Public Summary | ||
public |
catchError(selector: function): Observable Catches errors on the observable to be handled by returning a new observable or throwing an error. |
|
public |
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
import {catchError} from '@reactivex/rxjs/es6/operators/catchError.js'
Catches errors on the observable to be handled by returning a new observable or throwing an error.
Params:
Name | Type | Attribute | Description |
selector | function | a function that takes as arguments |
Return:
Observable | An observable that originates from either the source or the observable returned by the
catch |
Example:
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
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, ...
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
import {tap} from '@reactivex/rxjs/es6/operators/tap.js'
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.
Return:
Observable | An Observable identical to the source, but runs the specified Observer or callback(s) for each item. |
Example:
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));