RXJS Continue with concat subscribe after error
You can apply the catch
operator to each of the source observables and can perform the error logging within it. Like this:
const sources = [
Rx.Observable.from("1").delay(200),
Rx.Observable.from("2").delay(150),
Rx.Observable.throw("error"),
Rx.Observable.from("3").delay(124),
Rx.Observable.from("4").delay(201),
];
const sourcesWithCatch = sources.map(s => s.catch(e => {
console.log(e);
return Rx.Observable.empty();
}));
const concatted = Rx.Observable.concat(...sourcesWithCatch);
concatted.subscribe(v => console.log(v));
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://unpkg.com/rxjs@5/bundles/Rx.min.js"></script>
For future reference, Rxjs 5 has the onErrorResumeNext
function that behaves like the Visual Basic On Error Resume Next
statement.
This is the sample from the documentation
var source = Rx.Observable.onErrorResumeNext(
Rx.Observable.just(42),
Rx.Observable.throw(new Error()),
Rx.Observable.just(56),
Rx.Observable.throw(new Error()),
Rx.Observable.just(78)
);
var subscription = source.subscribe(
data => console.log(data)
);
// => 42
// => 56
// => 78