Javascript 内部带有订阅的Angular 6等待方法

Javascript 内部带有订阅的Angular 6等待方法,javascript,angular,typescript,http,asynchronous,Javascript,Angular,Typescript,Http,Asynchronous,我必须多次调用一个服务,forEach是一个内部有subscribe(http调用)的方法,每次调用都必须等待上一次调用的结束。 代码如下: itemDefaultConfiguration.command = (onclick) => { this.createConfiguration(configuration.components); //wait the end of createConfiguration method that has subscribe (htt

我必须多次调用一个服务,forEach是一个内部有subscribe(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); 
            });
}
你能帮我吗?谢谢

您可以使用以下方法完成此操作:

您可以使用以下方法完成此操作:


您可以使用以下语法实现这一点:

初始化所有组件(组件:代理[]){
for(设i=0;i

请小心使用
async/await
,因为您需要使用本机
for
循环来等待前一个调用-
forEach
reduce
,类似的方法只是触发多个异步调用。

您可以使用语法来实现:

初始化所有组件(组件:代理[]){
for(设i=0;i

小心使用
async/await
,因为您需要使用本机
for
循环来等待前一个调用-
forEach
reduce
和类似的方法只是触发多个异步调用。

如果它是
可观察的
,请尝试使用
开关映射
操作符:

import { switchMap } from 'rxjs/operators';
import { of } from 'rxjs/observable/of';

initializeAllComponents(components: Agent[]) {
    components.forEach(agent => {
      this.componentService.setAgent(agent).pipe(
           switchMap(res => {
               if (res) { return this.userService.get(user.uid); }

               return of(null);
           }));
      );
      this.componentService.clearAnyOneOfIndex();          
    })
}
如果它不可观察
,则使用
异步/等待
运算符:

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

如果它是可观察的
,尝试使用
开关映射
操作符:

import { switchMap } from 'rxjs/operators';
import { of } from 'rxjs/observable/of';

initializeAllComponents(components: Agent[]) {
    components.forEach(agent => {
      this.componentService.setAgent(agent).pipe(
           switchMap(res => {
               if (res) { return this.userService.get(user.uid); }

               return of(null);
           }));
      );
      this.componentService.clearAnyOneOfIndex();          
    })
}
如果它不可观察,则使用
异步/等待
运算符:

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


你试过什么?你需要付出一些努力,而不是要求我们为你做这件事。乍一看,您可能应该重新考虑该策略。我尝试了async/await,但没有改变,我认为有一种方法可以实现RxJS@please显示
selectComponent
methodo订阅日期您尝试了什么?你需要付出一些努力,而不是要求我们为你做这件事。乍一看,您可能应该重新考虑该策略。我尝试了async/await,但没有改变,我认为有一种方法可以实现RxJS@pleaseshow
selectComponent
methoduodate和subscribebeit不等待调用结束,这意味着setAgent在调用结束之前返回数据!是的,setAgent调用另一个具有subscribeTry的方法,使setAgent在setAgent中返回订阅本身,这里使用wait setAgent()。toPromise()forEach不等待,您需要使用普通for循环,请参阅我的答案。它不等待调用结束,这意味着setAgent在之前返回数据!是的,setAgent调用另一个具有subscribeTry的方法,以使setAgent在setAgent中返回订阅本身,这里使用wait setAgent()。toPromise()forEach不等待,您需要使用普通for循环,请参阅我的答案。不幸的是,在第一篇文章中没有看到它,setAgent调用另一个内部有subscribe的方法。如果无法观察到,则在第一篇文章中,setAgent调用另一个内部有subscribe的方法