问题
I have http interceptor. In that interceptor, before I change the request, I need a loader to turn on.
What really worries me is that I end up having lots of switchmaps.
why?
- loader is asynchronous
- I also need to translate the message passing from interceptor to loader service. translating message is also asyncrhonous. and in interceptor, i should run the request when loader and translating finishes
What I do in my loader service
showLoader(message){
return this.translateService.get(message).pipe(switchMap( (translatedMessage)=>{
this.loader$ = from(this.loadingController.create({message: translatedMessage}));
return this.loader$.pipe(switchMap((loader)=>{
return from(loader.present());
}));
}));
}
in my interceptor
intercept(request: HttpRequest<any>, next: HttpHandler) {
return this.loaderService.showLoader("WAITING").pipe(
take(1),
switchMap( ()=>{
so there are already 3 switchmaps nested. and below it, i need 2 or 3 more switchmaps (one for getting tokens from storage and one for something else). basically end up with having 5 switchmaps.
Question: Is nesting all these switchMaps bad practice?
来源:https://stackoverflow.com/questions/54638321/is-it-bad-practice-to-use-many-nested-switchmap