Upon initialization, I make 4 HTTP requests for data. After that, I need to load the data from the server to fill the forms based on the data model of the last 4 HTTP calls.
In simpler terms, I need to subscribe to these 4 HTTP calls and ensure they do not fail before finally making the 5th HTTP call to fetch data from the server.
From what I understand, I should avoid nesting observables and instead use switchMap, but how can I achieve this with 4 HTTP calls? Should I create an observable to wait for the HTTP calls and then use switchMap if they succeed for the 5th HTTP call?
This is the code snippet:
ngOnInit() {
this.service.getProvince().subscribe(
(value) => { return value; },
(error: AppError) => {
if (error instanceof NotFoundError) {
console.log('Error requesting HTTP');
} else {
console.log(error);
}
});
// Other similar HTTP request subscriptions...
}
// This is the 5th call that needs to be done only if the last 4 calls did not fail
finalCall {
this.service.getDomanda().subscribe((domanda: any) => {
this.populateForm(domanda); // Method that uses data to fill forms
},
(error: AppError) => {
if (error instanceof NotFoundError) {
console.log('Error requesting HTTP');
} else {
console.log(error);
}
});
}