【发布时间】:2020-02-01 17:10:09
【问题描述】:
我必须多次调用一个服务,通过 forEach,一个内部有订阅(http 调用)的方法,我每次都必须等待上一次调用结束。 这是代码:
itemDefaultConfiguration.command = (onclick) => {
this.createConfiguration(configuration.components);
//wait the end of createConfiguration method that has subscribe (http call) inside
this.initializeAllComponents(configuration.components)
};
initializeAllComponents(components: Agent[]) {
components.forEach(agent => {
this.componentService.setAgent(agent);
this.componentService.clearAnyOneOfIndex();
// wait the end of setAgent method that has subscribe(http call) inside
})
}
componentService 内部:
setAgent(agent: Agent) {
this.agent = agent;
this.selectComponent(agent.name, undefined, "/", "/", environment.maxLevelJsonSchema);
}
并且 selectComponent 有订阅。
selectComponent(agentName: string, propName: string, schemaPath: string, dataPath: string, deepLevel: number) {
this.getComponentSchema(this.agentName, schemaPath, deepLevel)
.subscribe((responseSchema) => {
this.getDataFailure(propName, dataPath, responseSchema);
});
}
你能帮帮我吗?谢谢
【问题讨论】:
-
你试过什么?您需要付出一些努力,而不是要求我们为您做这件事。乍一看,您可能应该重新考虑该策略。
-
我尝试了 async/await 但没有改变,我认为 RxJS 有办法
-
@请显示
selectComponent方法 -
与订阅者约会
标签: javascript angular typescript http asynchronous