0

I need to chain Observables, so after one is done then do next one. And i really want to do it right. I know there are more than one ways of doing this. And from what i have red switchMap() shout work fine for me.

But is it ok to set variables in tap()? It is supposed to be used for debugging. I think best practice is to set all in the subscribe(), but I`m not sure how to pass all needed params through the chain. First for the next request and then to the local variables.

    this.route.paramMap.pipe(
      map((params) => Number(params.get('id'))),
      switchMap(id => this.supplierService.get(id)),
      catchError((err) => {
        console.log(err);
        return of(undefined);
      }),
      tap((supplier) => {
        this.model = supplier;
      }),
      switchMap(() => {
        if (this.model?.defaultId !== undefined && this.model?.defaultId > 0) {
          return this.qmApiClient.getAnswers(this.model?.defaultId );
        }
        return of(undefined);
      }),
      catchError((err) => {
        console.log(err);
        return of(undefined);
      }),
      tap((q) => {
        this.secondObjResult= q;

        if (this.secondObjResult&& this.secondObjResult.categories) {
          this.contactDetailCategory = this.secondObjResult?.categories[0];
        }
      }),
      takeUntil(this.onDestroy$)
    ).subscribe(() => {  });;

Jackie
  • 327
  • 2
  • 13
  • 1
    You might find [this question](https://stackoverflow.com/q/63386585/1858357) helpful. – BizzyBob Mar 23 '23 at 11:41
  • @BizzyBob Thanks! This is good example. Any thought on where to store he local variables? Again in the tap() or? – Jackie Mar 23 '23 at 14:05

0 Answers0