I read a lot about switchmap and its purpose but I did not see a lot of examples when it comes to subscribing to the new data. So I use a nested subscription in my Angular project and I wanted to ask you how to use switchmap properly in my example to understand the concept better.
Here my nested subscription:
this.sharedSrv.postDetail.subscribe(async post => {
if(post) {
this.hasPost = true;
this.post = post;
}
console.log(post);
this.viewedMainComment = null;
this.viewedSubComments = [];
this.userSrv.getUserAsObservable().subscribe(user => {
if(user){
if(this.post.user.id == user.id) this.isOwnPost = true;
this.user = user;
this.postsSrv.getPostInteraction(this.user.id, this.post.id, this.post.user.id).subscribe(interaction => {
this.hasSubscribed = interaction["hasSubscribed"];
this.wasLiked = interaction["wasLiked"];
this.wasDisliked = interaction["wasDisliked"];
this.hasSaved = interaction["hasSaved"];
});
console.log(user);
this.isLoggedIn = true
} else {
this.isLoggedIn = false;
}
})
})
How would I use switchmap correctly here? Any help is appreciated.