I do not understand from the docs. Could anyone explain it to me?
The Angular Tap RxJs operator returns an observable that is identical to the source. It does not modify the stream in any way. Tap operator is useful for logging the value, debugging the stream for the correct values, or perform any other side effects.
The most common use of tap is actually for debugging. You can place a tap(console. log) anywhere in your observable pipe , log out the notifications as they are emitted by the source returned by the previous operation.
RxJS operators facilitate us to change the original observable in some manner and return a new observable. The operators do not change the existing observable. They simply modify it and return a new one. Operators are known as the type of functions that do not modify the variables outside of its scope.
tap and do are same RxJS operators. RxJS tap and do are same operator. In Angular 6+, we need to use tap operator. tap /do performs side effects for every value emitted by source Observable and returns an Observable identical to the source Observable until there is no error.
Most of the operators are working in streamed sequence, for example:
source$.pipe( map((a: string) => changeAndReturnArray(a)), filter((b: string[]) => giveMeOnlySymbolsThatAreAfterNInAlphabet(b)), switchMap((c: string[]) => putToSomeObservable(c)) .... );
In that example you are not 'breaking' the stream, or jumping outside of it to do some external action. Jumping outside of stream is possible with 'tap' operator, where you can:
My personal opinion - use 'tap' only if you can't find any better solution. Jumping outside of stream and calling some side effect can be double edged sword, especially when your dealing with some bigger application. Side effect are always harder to maintain, and you can finish with application that is doing magic stuff without any reason.
You can use it to perform a side effect for example. Or you can use it to see what's the current value that is being passed around without affecting/modifying the Observable. So something like a console.log()
but inside the stream.
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