Is it bad practice to use many nested switchMap?

自闭症网瘾萝莉.ら 提交于 2019-12-10 16:17:37

问题


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?

  1. loader is asynchronous
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!