gpt4 book ai didi

javascript - 内部订阅的Angular 6等待方法

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:21:11 25 4
gpt4 key购买 nike

我必须通过 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
})
}

内部组件服务:

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);
});
}

你能帮帮我吗?谢谢

最佳答案

您可以使用 await 来做到这一点,比如:

initializeAllComponents(components: Agent[]) {
components.forEach(async agent => {
await this.componentService.setAgent(agent).catch(console.log);
this.componentService.clearAnyOneOfIndex();
// wait the end of setAgent method that has subscribe(http call) inside
})
}

关于javascript - 内部订阅的Angular 6等待方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58218157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com