Ok, so I have the following problem. I have an api that in order to display the appropiate data to the front end I need to make three calls. The first call produces a number that the second call needs to retrieve the information necessary to access the data on the third call. The way I worked my way around this goes as follows:
const nastyAsyncFunc = () => {
//First call responds with necessary parameter for second call
axios.get("/api/endpoint1").then((response) => {
const endpoint1Data = response.data;
// Second call takes first parameter and produces second parameter as a response to access third call
axios.get(`/api/${endpoint1Data}/endpoint2`).then((response) => {
const endpoint2Data = response.data;
// With second response we can access third response data.
axios.get(`/api/${endpoint2Data}/endpoint3`).then((response) => {
const finalResponse = response.data;
return finalResponse;
});
});
});
};
This approach gets me the data that I need but as you can see it's hellish and it makes error handling a pain in the neck, to put it mildly. Have any of you encountered such a call and what was your approach to make it cleaner and easier to debug? as well as what is the standard approach in which to handle such requests. Thank you all in advance.