【发布时间】:2018-08-15 13:52:44
【问题描述】:
我有一个现有的函数,它调用 HTTP 端点来处理一些工作,该端点负责管理我的 Angular 组件中的一些逻辑。出现了一个要求,因此在某些情况下,我们需要调用所有现有的逻辑,还需要进行另一个 HTTP 调用。以下是我尝试的注释版本。
public resolveComponent(action: string) {
//if called with no parameters, don't do anything
//otherwise, call one of the two endpoints.
let preAction = (x: any) => {
if (action === "replay") {
return this.remediationService
.replayRemediation(this.workflow.Id);
}
else if (action === "remove") {
return this.remediationService
.removeRemediation(this.workflow.Id);
}
else {
return of([]);
}
}
this.remediationService
.resolveComponent(component)
.pipe(
//the line below was what I added after new requirement
mergeMap(preAction),
mergeMap(x => {
//grabs updated data from server
return this.remediationService.remediationComponent(component.Id);
})
)
.subscribe((x: SomeModel) => {
//logic happens
});
}
这按预期执行工作,但这是有条件地链接可观察对象的最佳方式吗?
【问题讨论】:
-
你可以尝试 switchMap 根据条件返回不同的 observables
-
他不会使用switchmap,因为他正在等待响应,exhaustmap会是更好的选择。回到主题:我认为这是一种像你一样做的合法方式