简体   繁体   English

未调用 RxJS5 终结运算符

[英]RxJS5 finalize operator not called

I'm trying to trigger a callback when all my observables are executed.当我所有的 observable 被执行时,我试图触发一个回调。 In my other, older project i used finally like so and that worked like a charm:在我的另一个较旧的项目中,我finally使用了这样的方法,并且效果很好:

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

But now I'm using a newer version of RxJS with Pipeable operators , but the finally call (now renamed to finalize ) never gets executed.但是现在我使用的是带有Pipeable 运算符的更新版本的 RxJS,但是finally调用(现在重命名为finalize )永远不会被执行。 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 => ...);

Any help is appreciated.任何帮助表示赞赏。

In observables, firing and completing are not the same thing.在 observables 中,触发和完成不是一回事。

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.即使每个项目都发出一个值,route.queryParams 根据定义永远不会完成,因为这是 Angular 实现它的方式,作为非终止可观察对象。 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 仅在其内部组合的每个 observable 完成时才会完成。

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 => ...);

This will complete.这将完成。

Are you sure one of combined Observable s actually completes?您确定组合Observable之一确实完成了吗? With either .complete or .error ?使用.complete.error

If none of the combined Observable s completes, finally will never be called.如果组合的Observable没有一个完成, finally将永远不会被调用。

If you want to do something when the observable completes then use the complete callback instead of finally/finalize:如果你想在 observable 完成时做一些事情,那么使用完整的回调而不是 finally/finalize:

.subscribe(
    value => { console.log(`Next: ${value}`); },
    error => { console.log(`Error: ${error}`); },
    ()    => { console.log(`Completed`); }
);

Anyway finally/finalize should also work and will be called on error OR complete.无论如何,finalize/finalize 也应该工作,并且会在错误或完成时被调用。 Im pretty sure that your observable never completes.我很确定您的 observable 永远不会完成。 You can confirm this by using my code above.您可以使用我上面的代码来确认这一点。

I see you are using Angular and subscribing to this.route.queryParams which never completes.我看到您正在使用 Angular 并订阅从未完成的this.route.queryParams You may create a new observable by use of first() so you get the value and it immediatly completes: this.route.queryParams.pipe(first())您可以使用first()创建一个新的 observable,以便您获得值并立即完成: this.route.queryParams.pipe(first())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM