I'm hoping someone can offer a little advice on how to do the following:
I have a requirement to chain multiple http requests with each one dependant on the result of the previous.
i.e
Step 1: Login User
Step 2: Authenticated Http request
Step 3: Authenticated Http request with response value from Step 2
Step 4: Authenticated Http request with response value from Step 3
So far I have the following code (which works):
this.authSandbox.login(this.loginForm).subscribe(() => {
this.customerEnquirySandbox.createCustomer(newCustomer)
.filter(response => !!response) // continue when response not null
.subscribe((response) => {
let response1Details = {
"id": response.data.id,
};
this.customerEnquirySandbox.createMeasure(response1Details )
.filter(response => !!response)
.subscribe((response) => {
let response2Details = {
"id": response.data.id,
};
this.customerEnquirySandbox.saveAnswers(response2Details )
.filter(response => !!response)
.subscribe((response) => {
if (response.data.success) {
alert('Form completed and answers saved successfully');
} else {
alert('Error submitting answers');
}
this.authSandbox.customerEnquirylogout();
});
});
});
});
The above works but doesn't seem like the most appropriate way. I've seem suggestions using switchMap but am not sure if this is the right approach. If anyone can offer any guidance on how to do the above more correctly then it will be much appreciated.
Thanks in advance