First of all, I want to make it clear that I know the difference between Rx throwError operator and JS's throw keyword. I just wanted to know why would I use the throwError operator? What am I gaining by creating a new observable that all it does is to throw an error the second anyone subscribes to it?
Long story short, why would I want to do this:
.catchError(err => throw "error!!")
Over this:
.catchError(err => throwError("error!!"))
Thanks!
If you just throw an error, it will end the pipeline. If you use throwError
, it will return a new observable that is going to emit that error, essentially will pass it on down the pipeline. throwError(error)
is equivalent to this:
return new Observable(observer=>{
observer.error(error)
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With