want to optimize the following code: multiple subscription in multiple if else statements.
consider:-
getList(): void {
this.subs.sink = this.subscription1.subscribe((user) => {
if (user) {
this.method1();
}
});
}
method1() {
//code
//code
if (condition){
//code
} else {
this.method2()
}
method2(){
this.subs.sink = this.subscription2.subscribe(
(response) => {
if(){
//code
} else {
this.method3();
}
}
method3(){
this.subs.sink = this.subcription3.subscribe(
(response) => {
//code
}
}
this resulting in triggering multiple subscription.
Any help. Appreciated.
There are multiple ways to streamline the multiple subscriptions. It depends what exactly the
// coderepresents. If most of the// codeare same, then you could possibly usefilterto apply the condition.The following method assumes each
// codeblock is unique. It usedswitchMapto map from one observable to another. If you do not wish to forward the observable to the subscription'snextblock, you could return RxJSEMPTYconstant. It would essentially complete the observable.