RxJS Crash Course – catch and retry operators

RxJS Crash Course –  catch and retry operators
catch and retry operators

.catch operator

Now we’re going to look at the .catch() for handling errors. In this example, I create a new observable from scratch but in the middle of it I throw an error.

When we get errors in the real world we want to be able to catch them and then do some error handling.

In this case, we’ll use the catch operator to complete the observable and just print out the error. So, notice that we get the first three values in the observable. But the very last one doesn’t get emitted.

Overall the idea is very similar to how promises work in JavaScript.

.catch operator

We also have a useful retry operator that will rerun the observable as many times as we want. When an error has occurred so right after catch we can add retry.

And in this case, we’ll retry it twice. When we run it. This time we’ll get the same sequence of events but they’ll happen three times.

The initial run plus the two retries so that’s all the operators.

I’m gonna cover for today there’s tons more in RxJS but I think these are some of the most useful and powerful ones that you’ll find.  Now let’s move to the last article of RxJS basic :