RXJ在多个可观察到完成后完成工作
假设我有3个可观察的job1()
,job2()
和job3()
。我想在所有这3个作业完成后,在finaljob()
取决于所有3个作业之后,我想调用finaljob()
。据我所知,我可以通过使用observable.forkjoin()
如下:
forkJoin([this.job1(), this.job2(), this.job3()])
.subscribe(([result1, result2, result3]) => {.
this.job1Completed = true;
this.job2Completed = true;
this.job3Completed = true;
this.finalJob();
});
但是我想同时运行这3个作业,以便这些工作都可以设置自己的每个作业完成状态完成工作后立即完成,而不是等待另外2个作业完成,然后将完整状态设置在一起,如下所示:
this.job1().subscribe((result1) => this.jodb1Completed = true);
this.job2().subscribe((result2) => this.jodb2Completed = true);
this.job3().subscribe((result3) => this.jodb3Completed = true);
finalJob(); // this has no guarantee all 3 jobs has completed
使用上面的实现,我将无法调用finaljob()
finaljob()< /code>在所有3个作业完成后。一旦所有作业完成,有什么方法可以发出事件来调用
finaljob()
?
Let's say I have 3 observables job1()
, job2()
, and job3()
. I would like to call the finalJob()
after all these 3 jobs has completed as the finalJob()
is dependent on all 3 jobs. From what I know, I can achieve this by using Observable.forkJoin()
like below:
forkJoin([this.job1(), this.job2(), this.job3()])
.subscribe(([result1, result2, result3]) => {.
this.job1Completed = true;
this.job2Completed = true;
this.job3Completed = true;
this.finalJob();
});
But the thing is I would like to run these 3 jobs simultaneously, so that each of these jobs can set their own completion status right after it has completed it's job, rather than waiting for another 2 jobs to complete then set the completion status all together, like below:
this.job1().subscribe((result1) => this.jodb1Completed = true);
this.job2().subscribe((result2) => this.jodb2Completed = true);
this.job3().subscribe((result3) => this.jodb3Completed = true);
finalJob(); // this has no guarantee all 3 jobs has completed
With the implementation above, I will not be able to call the finalJob()
after all 3 jobs has completed. Is there any way to emit an event to call the finalJob()
once all jobs has done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用rxjs
tap
操作员You can use the rxjs
tap
operator例如,使用
最终确定
。或tap
,如果您希望在每个发射中发生事情(编辑:和/或如果您对发射值感兴趣)而不是完成(如果这样做任何区别)。With
finalize
, for example. Ortap
, if you want things to happen on every emission (edit: and/or if you are interested in the emitted values), not on completion (and if that makes any difference).