RxJS5终结运算符未调用 [英] RxJS5 finalize operator not called

查看:114
本文介绍了RxJS5终结运算符未调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在所有可观察对象都执行后触发回调.在我的另一个较旧的项目中,我像这样使用finally,并且像一个符咒一样工作:

I'm trying to trigger a callback when all my observables are executed. In my other, older project i used finally like so and that worked like a charm:

this.myService.callDummy()
  .finally(() => console.log('Works!'))
  .subscribe(result => ...)

但是现在我正在使用带有 Pipeable运算符的RxJS的较新版本,但是finally调用(现在已重命名为finalize)从未执行过.几乎找不到信息,而且我不确定自己在做什么错.

But now I'm using a newer version of RxJS with Pipeable operators, but the finally call (now renamed to finalize) never gets executed. There is little information to be found and I'm not sure what I'm doing wrong.

combineLatest(
  this.route.queryParams,
  this.myService.callDummy1(),
  this.myService.callDummy2()
)
.pipe(finalize(() => console.log('Does not work!')))
.subscribe(results => ...);

感谢您的帮助.

推荐答案

在可观察对象中,触发和完成不是同一回事.

In observables, firing and completing are not the same thing.

即使每个项目都发出一个值,按定义,route.queryParams也将永远不会完成,因为这是Angular实现它的方式,是一个不间断的可观察性.您将需要手动完成它以完成执行,因为CombineLatest仅在其内部的每个可观察组合都完成时才完成.

Even though each of the items emits a value, route.queryParams by definition will never complete since that is how Angular implements it, as a non terminating observable. You will need to manually complete it for your finalize to execute since combineLatest will only complete when EVERY observable being combined inside of it has completed.

combineLatest(
  this.route.queryParams.pipe(take(1)), // take(1) will complete the observable after it has emitted one value
  this.myService.callDummy1(),
  this.myService.callDummy2()
)
.pipe(finalize(() => console.log('Does not work!')))
.subscribe(results => ...);

这将完成.

这篇关于RxJS5终结运算符未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆