Ionic2 / angular:如何知道2个不同的可观测量何时终止?

时间:2017-08-26 15:01:39

标签: rxjs reactive-extensions-js

这里有一些初学者问题:在Ionic2组件中,我使用Observables进行了2次不同的服务调用:

  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何时成功,我可以调用另一种方法(此方法需要chargeEvents和receiptsEvents数据)。

感谢。

1 个答案:

答案 0 :(得分:2)

您可以等待两个observable完成并使用forkJoin operator获取它们发出的值。它们仍将并行执行。

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