Is there something like tap in RxJS that ignores notification type?

In general tap pipe is for side-effects such as logging. In my case I just want to set isLoading property to false. The key is this place shouldn’t care whether it’s next or error type of notification but still tap needs to have it distinguished to work so I need to have duplicated code:

something.pipe(
    tap({
        next: () => {
            this.isLoading = false;
        },
        error: () => {
            this.isLoading = false;
        }
    }),
)

Is there any pipe, or some way to configure tap so I just provide one callback function which would run no matter what the notification type is? Eg.

something.pipe(
    anyTap(() => {
        this.isLoading = false;
    }),
)

And whatever something returns, anyTap would run it’s callback function anyway.

>Solution :

something.pipe(
  finalize(() => {
    this.isLoading = false;
  });
)

Leave a Reply