on ngOnInit i make 4 http request for data, after that i need to load the data from server to fill the forms with the data based on data model of last 4 http call.
In short words i need to subscribe to this 4 http calls, and make sure don't fail if they don't fail finally i can call the 5th http call to get data from server.
From what i understand, i should avoid to inest a observable and go with switch, but how to do this with 4 http call? should i create an observable wait for the http calls and if succed to use switchmap on the 5th http call?
This is the code.
ngOnInit() {
this.service.getProvince().subscribe(
(value) => { return value; },
(error: AppError) => {
if (error instanceof NotFoundError) {
console.log('Error richiesta http');
} else {
console.log(error);
}
});
this.service.getLingueStraniere().subscribe(
(value) => { return value; },
(error: AppError) => {
if (error instanceof NotFoundError) {
console.log('Error richiesta http');
} else {
console.log(error);
}
});
this.service.getTitoliPreferenziali().subscribe(
(value) => { return value; },
(error: AppError) => {
if (error instanceof NotFoundError) {
console.log('Error richiesta http');
} else {
console.log(error);
}
});
this.service.getRiserve().subscribe(
(value) => { return value; },
(error: AppError) => {
if (error instanceof NotFoundError) {
console.log('Error richiesta http');
} else {
console.log(error);
}
});
}
// this is the 5th call that need to be done only if last 4 call not fail
finalCall {
this.service.getDomanda().subscribe((domanda: any) => {
this.popolaForm(domanda); // Method that use data to fill foms
},
(error: AppError) => {
if (error instanceof NotFoundError) {
console.log('Error richiesta http');
} else {
console.log(error);
}
});
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…