Reputation: 2784
I am trying to wait to resolve a 1st promise, then 2 async promises, then fire off logic working from the all the prior promises.
How can I accomplish this? This is what I have so far, and the axios.all(promises)
does not wait for the prior 2 async promises.
fetchData = () => {
let promises = [];
axios.get("/api1.json")
.then((response) => {
//do logic 1
})
.then( () => {
promises.push(
() => { return
//data for components
axios.get("/api21.json")
.then(response => { //do logic 2.1 })
}
)
,
promises.push(
() => { return
axios.get("/api22.json")
.then(response => { //do logic 2.2 })
}
)
})
axios.all(promises).then( //do final logic 3 after logic 2.1 and 2.2 have performed ))
}
Upvotes: 1
Views: 177
Reputation: 936
The probem is that you are trying to run axios promise to fast, also the axios.get is too deep in the function. Could you try this one:
fetchData = () => {
axios.get("/api1.json")
.then((response) => {
//do logic 1
})
.then( () => {
return axios.all([
//data for components
axios.get("/api21.json")
.then(response => { //do logic 2.1 }),
axios.get("/api22.json")
.then(response => { //do logic 2.2 })
]);
})
.then( // do final logic 3);
}
Upvotes: 2
Reputation: 112777
You can do the first request, then use Promise.all
to wait for the last two promises to resolve before you do anything with the three responses.
Example
fetchData = () => {
axios.get("/api1.json").then(response1 => {
Promise.all([axios.get("/api21.json"), axios.get("/api22.json")]).then(
([response2, response3]) => {
console.log(response1, response2, response3);
}
);
});
};
Upvotes: 3