rxjs最终确定操作员vs tap({finalize:(()=> {}})

发布于 2025-02-11 09:12:55 字数 382 浏览 2 评论 0原文

A和B之间有任何区别吗?是否有任何情况会与另一种情况不同?

a)

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

b)

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

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
            })
        })
    )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心不设防 2025-02-18 09:12:55

TAP可以让您在可观察到的各种排放量产生副作用时,让您进入源可观察的一堆事件

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

,将它们全部分组在一起的单个tapobserver可能会更容易。否则,仅使用最终确定操作员可能会更清楚。


有任何情况会与另一种情况不同吗?

他们的行为不应该有所不同。

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.

澉约 2025-02-18 09:12:55

在TAP中最终确定对我来说绝对没有意义。

唯一的区别是,它将在最终完成之前调用。

Finalize in tap makes absolutly no sense to me.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文