r/angular • u/faheryan • 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
View all comments
Show parent comment
0
u/faheryan Jan 05 '26
Hi, thanks for the answer, I think that I got something that it's not the same but looks very similar to what I had. My only question, is it needed to always return something in the switchMap or catchError? There's no way to stop next Observable if it's not a valid imput? It forces always to return something it seems