Hello everyone!
I might have entered a nasty knot of observables and I don't know what is the best option to proceed with.
I have three requests:
- getUser, which returns a user.
- verifyEmail, which needs the user's email.
- sendEmail, which sends the user an email.
In the middle of all of it, I need to do something with the user itself.
I thought about the following:
this.userService.getUser().pipe(
switchMap(user => combineLatest([of(user], this.userService.verify(user.email)),
).subscribe(([user, emailResult]) => {
// Use the user.
setProfile(user);
this.router.navigate(['home']);
// Need to send an email.
this.userService.sendEmail(user).subscribe((email) => console.log('example done'));
})
But the second subscribe there feels very weird and I think it could be included in the pipe. But how?
I'm struggling because the 2nd and 3rd sources depend on the first one.
Extra question: What if the second request fails? I dont need to call the third if it does.