[英]React Resolve all promises
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. 我试图等待解析第一个Promise,然后解析2个异步Promise,然后从所有先前的Promise中解雇逻辑。
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. 到目前为止,这就是我所拥有的,而axios.all(promises)
不会等待之前的2个异步承诺。
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 ))
}
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. 您可以执行第一个请求,然后使用Promise.all
等待最后两个promise解析,然后再Promise.all
三个响应执行任何操作。
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);
}
);
});
};
The probem is that you are trying to run axios promise to fast, also the axios.get is too deep in the function. 该问题是您试图快速运行axios promise,而且axios.get在功能上太深了。 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.