Javascript 做出反应,兑现所有承诺
我正在尝试等待解决第一个承诺,然后是两个异步承诺,然后触发所有先前承诺的逻辑工作 我怎样才能做到这一点?这就是我到目前为止所拥有的,Javascript 做出反应,兑现所有承诺,javascript,reactjs,axios,Javascript,Reactjs,Axios,我正在尝试等待解决第一个承诺,然后是两个异步承诺,然后触发所有先前承诺的逻辑工作 我怎样才能做到这一点?这就是我到目前为止所拥有的,axios.all(promissions)并不等待前面的两个异步承诺 fetchData = () => { let promises = []; axios.get("/api1.json") .then((response) => { //do logic 1 })
axios.all(promissions)
并不等待前面的两个异步承诺
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 ))
}
您可以执行第一个请求,然后使用等待最后两个承诺得到解决,然后再对这三个响应执行任何操作 示例
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);
}
);
});
};
您可以执行第一个请求,然后使用等待最后两个承诺得到解决,然后再对这三个响应执行任何操作 示例
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);
}
);
});
};
问题是,您正在尝试运行axios promise to fast,而且axios.get函数太深。你能试试这个吗
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);
}
问题是,您正在尝试运行axios promise to fast,而且axios.get函数太深。你能试试这个吗
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);
}
您的
axios.all
将在您兑现承诺之前启动。。。你必须把它放在同一块。你的axios。在你把承诺推进去之前,所有的都会启动。。。你必须把它放在同一个街区。