Home > Enterprise >  Nested subscribe rxjs
Nested subscribe rxjs

Time:12-03

How to rebuilt this using rxjs if it is advisable:

constructor(private customerInfoService: CustomerInfoService) {
  customerInfoService.getCustomerIPById(this.a).subscribe(x => {
    customerInfoService.getIPActivityDates(x).subscribe(y => {
      this.latestActivityDate = y.latestDate;
    })
  })
}

CodePudding user response:

You can use switchMap. This allows you to "switch" the observable returned. So in your case, you could do something like:

customerInfoService.getCustomerIPById(this.a).pipe(
  switchMap(x => customerInfoService.getIPActivityDates(x))
}).subscribe(y => {
  this.latestActivityDate = y.latestDate;
})

CodePudding user response:

try this.

    constructor(private customerInfoService: CustomerInfoService) {
  this.customerInfoService.getCustomerIPById(this.a)
    .pipe(
      concatMap(x => this.customerInfoService.getIPActivityDates(x))
    ).subscribe(y => {
      this.latestActivityDate = y.latestDate;
    });
}
  • Related