RxJs stops listening to click events when an inner observable errors (Ajax request). I'm trying to figure out how to keep the event listener hooked to the button click event and gracefully handle the inner ajax error.
Here is my example code and a link to plunkr
var input = $("#testBtn");
var test = Rx.Observable.fromEvent(input,'click');
var testAjax = function() {
return Rx.Observable.range(0,3).then(function(x){
if(x==2)throw "RAWR"; //Simulating a promise error.
return x;
});
}
test.map(function(){
return Rx.Observable.when(testAjax());
})
.switchLatest()
.subscribe(
function (x) {
console.log('Next: ', x);
},
function (err) {
console.log('Error: ' + err);
},
function () {
console.log('Completed');
});
I ran into my own issues interpreting how this (and
onErrorResumeNext
) functioned. The struggle I encountered was with what context the catch (or resume next) applied to. The simple colloquial translation that makes sense for me is the following:The key take away is that your original source is interrupted and replaced with the observable(s) you provide in the
catch
/onErrorResumeNext
function. This means that if you had something like this:Then adding
.catch(Rx.Observable.return('N/A'))
or.onErrorResumeNext(Rx.Observable.return('N/A'))
will not actually just continue your stream (sourced by theinterval
), but rather end the stream with a final observable (theN/A
).If you are looking to instead handle the failure gracefully and continue the original stream you need to so something more like
.select(function(x) { return x.catch(Rx.Observable.return('N/A')); })
. Now your stream will replace any observable element in the stream that fails with a caught default and then continue on with the existing source stream.Here is a JSFiddle that shows this in action.
You can use the
catch
operator (or thecatchException
alias) to catch and handle errors which occur in an observable, so that subscribers are not notified of the error.Ignore Errors:
Handle Errors:
I was still a little confused after trying the accepted answer, so this is what ended up working for me. This is what I had:
When the server returned an error (like when the user already entered their email) I wasn't able to listen for the user's email form submit again. This is what worked for me: