Imagine having a piece of old code that you don't want to modify. There are three server calls involved in fetching data, and after all three methods succeed, you need to execute some additional code. I introduced a variable and now want to monitor it for changes (ngOnChanges is not an option).
Take a look at the following code snippet:
ngOnInit() {
this.loadedReq = 0; // This variable will reach 3 when all requests are successful, triggering custom code execution. How do I watch this variable?
this.getCars();
this.getModels();
this.getTypes();
}
getCars() {
return this.myService.getCars(this.clientId)
.subscribe((response) => {
this.loadedReq++;
});
}
getModels() {
return this.myService.getModels(this.clientId)
.subscribe((response) => {
this.loadedReq++;
});
}
getTypes() {
return this.myService.getTypes(this.clientId)
.subscribe((response) => {
this.loadedReq++;
});
}
someMethodWhenAllLoaded(){}
Is there a way to achieve this without altering the methods extensively (e.g., implementing complex RxJs logic on responses)?
If not, how can a workaround be created in this scenario?