RxJS "throws new error" against "Observable.throw"

Is there a difference between throw new Error()

and Observable.throw()

, and if so, what is it? Obviously since there is Observable.throw

, there is some reason for this, but other than the shorter stack trace, I couldn't find the difference.

Here's an example

+3


source to share


1 answer


Observable.throw

returns error on streams. For example, I'm expecting Observable

, but for some reason I want to return an error. In these cases, cannot be used throw new Error()

.

For example:

var source = someStream$
    .flatMap(data => data?
             Rx.Observable.of(data): // Or any other async operation 
             Rx.Observable.throw('invalid data'));

      



There flatMap

is a stream waiting for the Observable, if we need to use throw new Error()

, we need to do a hack.

It may have other advantages to work well with rx statements that I am not aware of.

+2


source







All Articles