我有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);
但是在保存条目时,它没有设置名称属性。
在保存条目之前,等待这些异步调用中的任何一个返回的正确方法是什么?
答案 0 :(得分:1)
如果必须使用switch语句,则可能让每个分支返回observable,然后在myObject.name
中设置subscribe()
或使用do和{{1}等运算符将值传递给switchMap
。
this.storageService.saveEntry()
希望这有帮助!