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(() => { });;