1
\$\begingroup\$

I have a number of async calls that get chained conditionally based on the previous data dependency. I.e. perform call A -> if the result is defined/not an error execute a side effect, if not perform another call.

I originally started with nested subscribes but I was told that is not a good practice. Next I moved to rxjs operations (which I believe are function compositions in this context) and used tap, as we have side-effects. This still looks pretty ugly, wondering if there are any suggestions to improve on this (One thing that I am aware of is I can abstract the general pattern into a

if(undefined) callback else side-effect

function. Appreciate any other feedback

this.userService.expandUser().pipe(
         tap((user: User) => { //flatMap equivilant of Scala
              if(user == undefined){
                this.userService.setUndefined();
                this.bandService.expandBand().pipe(
                  tap((band: Band) => {
                     if(band == undefined){
                       this.bandService.setUndefined()
                       this.shadowService.checkShadowsStatus().pipe(
                           tap(result => { 
                                if(!result){
                                  this.shadowService.createShadowUser().pipe(
                                      tap(result => {
                                        this.shadowService.shadowSet = true;
                                      })
                                    )
                                }else{
                                  this.shadowService.shadowSet = true;
                                }
                             })
                         )
                     }else{
                       this.bandService.setBand(band)
                     }
                  })
                )
              }else{
                this.userService.setUser(user)
              }
         })
      )
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.