Angular 将嵌套的promise.all替换为可观察流,以便在数组上聚合

Angular 将嵌套的promise.all替换为可观察流,以便在数组上聚合,angular,typescript,rxjs,Angular,Typescript,Rxjs,在Angular应用程序中,我们有一个方法将3个api结果合并到一个巨大的列表中 loadPlaces$=此.actions$.pipe ofTypePlaceActionTypes.LOAD\u位置, switchMapaction:LoadPlaces=> 来自this.service.findAreas.pipe switchMapasync区域:区域[]=>{ 试一试{ const places:Place[]=等待承诺 areas.mapasync区域:区域=>{ const[住宅,商

在Angular应用程序中,我们有一个方法将3个api结果合并到一个巨大的列表中

loadPlaces$=此.actions$.pipe ofTypePlaceActionTypes.LOAD\u位置, switchMapaction:LoadPlaces=> 来自this.service.findAreas.pipe switchMapasync区域:区域[]=>{ 试一试{ const places:Place[]=等待承诺 areas.mapasync区域:区域=>{ const[住宅,商业]=等待承诺[ this.service.getResidentialsarea.id, this.service.getCommercialarea.id, ]; 归还新区域、住宅区、商业区; } ; 返回新装货地点成功地点; }捕捉错误{ 返回新LoadPlacesFailerror; } }, catchErrorerror=>of new LoadPlacesFailerror ; 我们检索所有区域。然后,对于每一个,我们必须构建一个对象位置,其中包含该区域的信息+2其他住宅和商业信息

我们最终得到一个位置数组,每个位置都包含嵌套数据

事实上,我们的方法将停止回报承诺,并一直使用可观察的方法

我找不到一种方法将我当前的链转换为只使用可观察对象。我设法使用CombineTest同时获得住宅和商业,但在绘制我的区域阵列时,我迷路了。事实上,我必须返回每个区域的嵌套调用的结果,并且不知道如何让rxjs在不需要调用的情况下自动订阅观测值

如何实现每个项目嵌套的CombineTest订阅


我尝试了混合使用from和switchMap嵌套CombineTest,但没有成功。我总是得到一个可观察的数组,而不是值。

看起来是这样的,这是很多forkJoin->map

switchMap((action: LoadPlaces) =>
  from(this.service.findAreas()).pipe(
    switchMap((areas: Area[]) => {
      return forkJoin( // forkJoin 
        areas.map((area: Area) => { // map to observables 
          return forkJoin([ // forkJoin these too
            from(this.service.getResidentials(area.id)), // convert them to observables
            from(this.service.getCommercial(area.id)),
          ]).pipe( // map the result
            map(([residential, commercial]) => new Place(area, residential, commercial))
          );
        }) 
      ).pipe( // map this result too
        map(places => new LoadPlacesSuccess(places))
      );
    }),
    catchError((error) => of(new LoadPlacesFail(error)))
  )
)

请举例说明您尝试的具体问题。为什么这些服务方法会回报承诺?@bryan60这是一个旧的系统要求,然后我们转向了一个新的模式。我不知道如何绘制forkJoin内的区域。这很有帮助!我们是否必须在执行…areas.map时对其进行分解,因为我似乎只对第一个forkJoin中的第一个区域进行了一次调用?应该不需要进行分解。forkJoin内部的一切都要完成,这一点很重要,因此问题可能出在您的服务实现中。