如何等到许多可能的观察者之一返回后再继续

时间:2017-08-23 18:45:05

标签: angular rxjs observable angular-http

我有switch语句,根据输入进行不同的http调用。

它看起来像这样:

switch(myObject.type){
    case 'Type1':
        myObject.name = this.service1.getName(myObject.id);
        break;
    case 'Type2':
        myObject.name = this.service2.getName(myObject.id);
        break;
    case 'Type3':
        myObject.name = this.service3.getName(myObject.id);
        break;
}

在这些之后我立即发表了保存条目的声明:

this.storageService.saveEntry(myObject);

但是在保存条目时,它没有设置名称属性。

在保存条目之前,等待这些异步调用中的任何一个返回的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

如果必须使用switch语句,则可能让每个分支返回observable,然后在myObject.name中设置subscribe()或使用do和{{1}等运算符将值传递给switchMap

this.storageService.saveEntry()

希望这有帮助!