Home > OS >  rxjs avoid nested subscription with combineLatest
rxjs avoid nested subscription with combineLatest

Time:03-09

I need to fix this nested subscriptions , but i have some difficulties :-( could you help me please to find a solution?

combineLatest([stream1,stream2])
    .pipe(takeUntil(this.destroyed$))
    .subscribe({
      next: ([a, b]) => {
        this.extService.get(a,b).subscribe();
      },
    });

CodePudding user response:

In this case you would have to use switch map after, this way it will "change" to the new subscription

combineLatest([stream1,stream2])
    .pipe(
          takeUntil(this.destroyed$),
          switchMap(([a,b])=>this.extService.get(a,b)
     )
)
    .subscribe(
   (responseFromExtService) => { // your code}
);

Make sure you know also the difference between combine lastest and fork join

  • Related