I am using the Angular2 async pipe to stream values into the DOM. Here's a real simple example:
const stream = Observable.interval(1000)
.take(5)
.map(n => { if (n === 3) throw "ERROR"; return n; });
<div *ngFor="for num of stream | async">
{{num}}
</div>
<div id="error"></div>
What I would like to do is to have the sequence of 1-5 displayed, but on the error item (3), somehow populate the #error
div with the error message.
This seems to require two things: first is the ability of the Angular async pipe to do something intelligent with errors, which I see no sign of. Looking at the source code, apparently it throws a JS exception, which doesn't seem too friendly.
Second is the ability to restart or continue the sequence after the error. I have read about catch
and onErrorResumeNext
and so on, but they all involve another sequence which will be switched to on an error. This greatly complicates the logic of generating the stream, on which I would just like to put a series of numbers (in this simple example). I have the sinking feeling that once an error occurs the game is over and the observable is completed and can only be "restarted" with a different observable. I'm still learning observables; is this in fact the case?
So my question is twofold:
- Can Angular2's async pipe do something intelligent with errors?
- Do observables have some simple way to continue after an error?
Yes you're right regarding the catch operator and the ability to do something after errors occur...
I would leverage the
catch
operator to catch the error and do something:and in the template:
To be able to go on the initial observable, you need to create a new one starting at the point where the error occurs:
and use it in the
catch
callback:These two questions could help you:
1) no, The
async
pipe subscribes and unsubscribes and returns the events it receives. You would need to handle the errors before they receive theasync
pipe.2) You can use the catch operator and when it returns an observable then its value(s) is emitted by the
.catch(err => Observable.of(-1))
instead of the error.You could use this to emit a special "error" value and then use something like
*ngIf="num === -1
to show the error value in some special way.You can find more information on this https://blog.thoughtram.io/angular/2017/02/27/three-things-you-didnt-know-about-the-async-pipe.html