等待观察对象的值(作为列表)使用列表中的每个成员调用另一个观察对象?

时间:2019-09-18 21:20:31

标签: angular rxjs rxjs-observables

我正在为我的应用程序使用3个端点,其中1个端点取决于其他端点的响应(即一个列表),然后我需要使用列表中的每个项目才能使用另一个端点,我们称它们为epAepBepCepA返回一个列表,然后我在epB上使用此列表,类似epA.Foreach( x => epB(x)),我正在尝试将epBepC合并到一个合并列表中,因为两者共享相似的字段。

我的问题是,我对角度和可观测对象的使用太新了,我不知道是否有一种方法可以将这些epB和epC结果结合起来(不提及当前,我订阅了可观测对象并将其值分配给其他对象我需要的东西)...如果有人可以帮我,将不胜感激。抱歉,如果这太乱了,我在这里发布和角度编码的经验很少。

这是我目前拥有的一些代码...虽然有点丑陋,但确实可以完成工作

代码

this._serverRequests.epA(this._Token).subscribe(x => {
    this.servers = x;
    x.forEach(server => 
      this._serverRequests.epB(server)
      .subscribe(info => {
         this.serverInfo = info;
         this.GridModel.data = info['States'];
         this.GridModel.data.forEach(se => {
             se.Start = this.formatValuesPipe.transform(se.Start, 'grid');
         });
         this.GridModel.data.map( o => {
            o.ServerUrl = server;
         });
       })
     );
},
   error => this.errMsg = <any>error
);

this._serverRequests.epC(this._Token).subscribe(lic => {
   this.licensesList = lic;
   this.licensesModel.data = this.licensesList.LicenseUsageList;
   this.licensesModel.data.forEach(li => {
      li.AcquisitionTime = this.formatValuesPipe.transform(li.AcquisitionTime, 'grid'); 
   });
});

我也尝试了forkjoin,但是由于epA返回了一个列表,所以我不知道如何调用forkjoin内的每个项目

1 个答案:

答案 0 :(得分:0)

您可以将第一个调用的响应映射到一个调用数组,然后使用CombineLatest给出所有响应的数组。

this._serverRequests.epA(this._Token).pipe
  map(server => this._serverRequests.epB(server)),
).subscribe(requests => {
  combineLatest(requests).subscribe(results => {
    // You have an array of the multiple results here
  })
});
相关问题