RxJS throwError() Creation OperatorRxJS 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
Return valueThe 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: 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: 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: 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
|