RXJ在多个可观察到完成后完成工作

发布于 2025-02-10 13:55:42 字数 971 浏览 2 评论 0原文

假设我有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 技术交流群。

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

发布评论

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

评论(2

心意如水 2025-02-17 13:55:42

您可以使用rxjs tap操作员

forkJoin([
  this.job1().pipe(tap((result1) => this.jodb1Completed = true)),
  this.job2().pipe(tap((result2) => this.jodb2Completed = true)),
  this.job3().pipe(tap((result3) => this.jodb3Completed = true)),
])
.subscribe(response => {
  finalJob();
  console.log('done', respone)
});

You can use the rxjs tap operator

forkJoin([
  this.job1().pipe(tap((result1) => this.jodb1Completed = true)),
  this.job2().pipe(tap((result2) => this.jodb2Completed = true)),
  this.job3().pipe(tap((result3) => this.jodb3Completed = true)),
])
.subscribe(response => {
  finalJob();
  console.log('done', respone)
});
止于盛夏 2025-02-17 13:55:42

例如,使用最终确定。或tap,如果您希望在每个发射中发生事情(编辑:和/或如果您对发射值感兴趣)而不是完成(如果这样做任何区别)。

const stream1$ = this.job1().pipe(
    finalize(() => this.jodb1Completed = true)
);

const stream2$ = this.job1().pipe(
    finalize(() => this.jodb2Completed = true)
);

forkJoin([stream1$, stream2$]).subscribe(this.finalJob);

With finalize, for example. Or tap, 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).

const stream1$ = this.job1().pipe(
    finalize(() => this.jodb1Completed = true)
);

const stream2$ = this.job1().pipe(
    finalize(() => this.jodb2Completed = true)
);

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