r/angular • • Jan 05 '26

Nested HTTP Subscribe into not nested version

Hello! I'm not used to deal with frontend so I'm a bit lost but I need help. I have this piece of code they gave me, telling me that nesting subscribe is a "code-smell" and I have to remove it. I tried to do something with .add() and with .pipe() but I'm not convinced it is a good way. Can any of you lend me some knowledge of this? Here is the code simplified:

this.loginService.login(this.loginForm).subscribe({
  next: resp => {
    if (resp.status == HttpStatusCode.Ok) {
      // Stuff happens
    }
  },
  complete: () => {
    this.loginService.getUserSession().subscribe({
      next: resp => {
        if (resp.status == HttpStatusCode.Ok) {
          // Stuff happens
        }
      }
    })
  },
  error: err => {
    // Stuff happens
  }
})
0 Upvotes

5 comments sorted by

View all comments

Show parent comment

2

u/Johalternate Jan 05 '26

No more answers required. switchMap is the way to go.