Javatpoint Logo
Javatpoint Logo

RxJS tap() Utility Operator

RxJS tap() operator is a utility operator that returns an observable output that is identical to the source observable but performs a side effect for every emission on the source observable.

In other words, you can say that the RxJS tap() operator is used to intercept each emission on the source observable and runs a function but returns an output that is identical to the source observable as long as it doesn't find any error.

This operator is generally used for debugging observables for the correct values or performing other side effects.

Syntax:

Following is the syntax of the RxJS tap() utility operator:

Or

Parameter Explanation

  • observer or nextOrObserver: This specifies a normal observer object or a callback for next. This is the same as the source observable. It is an optional argument. Its default value is undefined.
  • error: It is used to specify the error method if any error occurs in the source. It is also an optional argument. Its default value is undefined.
  • complete: This is called when the task is complete. It is also an optional argument. Its default value is undefined.

Return value

The RxJS tap() operator's return value is an observable identical to the source observable with a callback function that runs the specified observer or callbacks for each item.

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

Example 1

Output:

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

RxJS tap() Utility Operator

Example 2 (Logging with tap)

Output:

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

RxJS tap() Utility Operator

Example 3 (Using tap with object)

Output:

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

RxJS tap() Utility Operator
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