Javatpoint Logo
Javatpoint Logo

RxJS throwError() Creation Operator

RxJS throwError() operator is a creation operator used to create an observable that returns no items to the observer and immediately emits an error notification.

In other words, we can say that the RxJS throwError() operator returns an observable that will notify an error. It just emits 'error' and nothing else.

The RxJS throwError() operator is mostly used to create a simple observable that only emits the error notification. It can be used for composing with other observables, such as in a mergeMap.

Syntax:

Following is the syntax of the RxJS throwError() creation operator:

Or

Or

Parameter Explanation

  • error: It is used to specify a particular error to pass to the error notification.
  • scheduler: It specifies a SchedulerLike to use for scheduling the emission of the error notification. It is an optional argument. Its default value is undefined.

Return value

The RxJS throwError() operator returns an observable that emits only the error notification using the given error argument.

Let us see some examples of the RxJS throwError() operator to understand it clearly.

Example 1 (Emit an Error)

Output:

After executing the above example, you will see the following result:

RxJS throwError() Creation Operator

In the above example, you can see that it has emitted an error message as "An error is occurred".

Example 2 (Emit some specific values, then emit an error)

Output:

After executing the above example, you will see the following result:

RxJS throwError() Creation Operator

In the above example, you can see that it has emitted the specified values i.e. 1, 2, 3, 4, 5 which we have specified in the example and then emit the error.

Example 3 (Map and flatten numbers to the sequence and then throw an error after the specified repetition)

Output:

After executing the above example, you will see the following result:

RxJS throwError() Creation Operator

In the above example, you can see that it has mapped and flatten numbers to the sequence 'a', 'b', 'c', and then throw an error after 3 repetitions.


Next TopicRxJS Operators





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA