Ionic2/angular:如何知道 2 个不同的 observables 何时终止?

Ionic2/angular: how to know when 2 different observables are terminated?

这里是初学者问题:在 Ionic2 组件中,我有 2 个使用 Observables 的不同服务调用:

  getTimelineEvents() {
    this.receiptsEventsRequestState = RequestState.Pending;
    this.chargesEventsRequestState = RequestState.Pending;

    this.miscService.getCustomerReceiptsEvents()
      .subscribe(
        (events: TimelineEvent[]) => {
         this.receiptsEventsRequestState = RequestState.Success;
         this.receiptsEvents = events;
         },
      );

    this.miscService.getCustomerChargesEvents()
      .subscribe(
        (events: TimelineEvent[]) => {
        this.chargesEventsRequestState = RequestState.Success;}
        this.chargesEvents = events;
      );

  }

我想知道 getCustomerReceiptsEventsgetCustomerChargesEvents 何时都成功,以便我可以调用另一个方法(此方法需要 chargesEvents 和 receiptsEvents 数据)。

谢谢。

您可以等待两个可观察对象完成并使用 forkJoin operator 获取它们发出的值。它们仍然会并行执行。

Observable.forkJoin(
  this.miscService.getCustomerReceiptsEvents(),
  this.miscService.getCustomerChargesEvents(),
)
  .subscribe(([receipts, charges] => {
    console.log('Results', receipts, charges)
  })