Open benlesh opened 4 months ago
(fwiw i think tap
is a much more intuitive and understandable name than do
, given that do
is a language keyword)
@ljharb: Interesting. RxJS originally had do
, and I think I pulled tap
out of my butt (or someone else's) because do
was a keyword. I'm indifferent on the name, TBH. If everyone likes tap
, that's less education for folks using RxJS, I suppose.
Some previous discussion in https://github.com/WICG/observable/issues/29
(On the subject of naming, Rust spells this .inspect
.)
I also like tap
and think it got some good reception in #29 as @bakkot points out, and it's especially promising that it might have a place in iterator helpers some day, under that name, given https://github.com/WICG/observable/issues/29#issuecomment-1656430426. With that, I think I'll move forward spec'ing tap()
as proposed here.
I would maybe call it inspect
, following Rust? That seems like a more obvious name. But either's ok.
That might be even better, especially since there is some language precedent with it.
do
is a simple but important method for Observable. It allows for repeatable side effects, and is commonly used for logging etc.An example use case would be if someone were to create some sort of shared connection observable and pass it around to users, such as a component that may be mounted more than once, but they want to log what's going through it.
Other information
For a VERY LONG TIME, RxJS only mirrored what you could do in
subscribe
indo
(akatap
in RxJS). However, there were many valid requests to be able to usedo
to inspect when you subscribe or unsubscribe. This isn't necessary atsubscribe
because you know when you subscribe when you actually callsubscribe
. Similarly theabort
(unsubscribe
in RxJS) callback is not required when the consumer aborts, because the consumer knows when it aborts, it just did it. It owns that code.This method is also one of the better ways to do debug logging so you can see what is happening over time, and is in popular use there. I'm actually surprised that Promise doesn't have a
do
. Withoutdo
, most people will just usesource.map(x => (/* side effect */, x))
, which is limited, not very semantic, and inefficient.