我试图等待解析第一个Promise,然后解析2个异步Promise,然后从所有先前的Promise中解散逻辑。
我该怎么做?这是我到目前为止的内容,并且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 ))
}
您可以先执行第一个请求,然后再使用Promise.all
等待三个最后的承诺解决,然后再对三个响应进行任何操作。
例
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);
}
);
});
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句