【发布时间】:2017-02-06 10:46:08
【问题描述】:
我正在拨打多个电话,需要等待所有电话都完成,然后才能转到其他页面。我遇到的问题是,通常我一直在嵌套 Web 服务调用,但在这种情况下,我有单独的调用,这些调用可能会根据用户的输入进行调用,也可能不会调用。在路由到新页面之前,如何等待所有可能会或可能不会被调用的呼叫。
submitButton_Clicked() {
this.busy = this.service.call1() // must be called
.first().subscribe(
if (success){
// **Logic to decide if call 2 is needed**
...
this.busy = this.service.call2() // might be called
.first().subscribe();
// **Logic to decide if call 3 is needed**
...
this.busy = this.service.call3() // might be called
.first().subscribe();
this.router('route'); // should route after all potential calls
}
);
}
我是 observables 的新手,所以我不确定最好的方法是什么。谢谢您的帮助!
【问题讨论】:
-
如果你使用了 Promise,你可以使用 Promise.all 来实现。但我可以看出这不是你想要的答案。
-
不是有条件地订阅每个,而是考虑有条件地将每个映射到它自己或一个空的 observable,然后将所有这些可能为空的流合并或连接到一个新的流中,然后调用 reduce 并订阅它。
标签: angular typescript rxjs observable