这里有一些初学者问题:在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;
);
}
我想知道getCustomerReceiptsEvents
和getCustomerChargesEvents
何时成功,我可以调用另一种方法(此方法需要chargeEvents和receiptsEvents数据)。
感谢。
答案 0 :(得分:2)
您可以等待两个observable完成并使用forkJoin
operator获取它们发出的值。它们仍将并行执行。
Observable.forkJoin(
this.miscService.getCustomerReceiptsEvents(),
this.miscService.getCustomerChargesEvents(),
)
.subscribe(([receipts, charges] => {
console.log('Results', receipts, charges)
})