简体   繁体   中英

Rxjs - How to call two httpService after combinelatest?

I don't know if it's the better solution but I need a function that subscribes to two ngrx selectors and use it as parameters for two http services and I used combineLatest:

  combineLatest([this.selectedCompany$, this.account$]).subscribe(res => {
  this.idCompany = res[0]!.id;
  this.account = res[1];
  this.getDashboardCardService(this.account!.id.toString(), this.idCompany.toString(), 'time');
  this.getavailableSpaceService(this.idCompany).subscribe(res => {
      this.space = res;
  })

}).unsubscribe();

I use combineLatest to subscribe to two ngrx selectors and in subscribe I need to use these values for doing two http calls, getDashboardCard and available. It works but I don't like it...which is the better solution for doing it?

In short, You should make .pipe( after combineLatest and inside that pipe use for example switchMap to be able to call another service.

Maybe you can use this to get you headed the right direction?

Here I use a higher order observable operator ( concatMap );

combineLatest([this.selectedCompany$, this.account$]).pipe(
  concatMap(([company, account]) => forkJoin([
    this.getDashboardCardService(
      account!.id.toString(), 
      company!.id.toString(), 
      'time'
    ),
    this.getavailableSpaceService(company!.id)
  ]))
).subscribe(([res1, res2]) => {
  this.space = res2;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM