0

Is there any difference between A and B? Are there any cases where one would behave differently than the other?

A)

observableHere
    .pipe(
        finalize(() => {
            // Do stuff here
        })
    )

B)

observableHere
    .pipe(
        tap({
            finalize: () => {
                // Do stuff here
            })
        })
    )
JWess
  • 602
  • 7
  • 17

2 Answers2

0

Finalize in tap makes absolutly no sense to me.

The only difference is that it will be called before the final finalize.

Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134
0

Tap lets you hook into a bunch of events on the source observable

interface TapObserver<T>: {
   next: (value: T) => void;
   error: (err: any) => void;
   complete: () => void;
   subscribe: () => void;
   unsubscribe: () => void;
   finalize: () => void;
}

If you're producing side effects for an observable's various emissions, it might be easier to group them all together in a single TapObserver. Otherwise, just using the finalize operator is probably clearer.


Are there any cases where one would behave differently than the other?

They shouldn't behave any differently.

Mrk Sef
  • 7,557
  • 1
  • 9
  • 21
  • As mentioned by Matthieu below, if `finalize` exists in both places, tap's `finalize` would be executed first. – JWess Jun 29 '22 at 15:40