【发布时间】:2019-03-15 10:44:52
【问题描述】:
我在异步函数中有异步函数。在第二个中,我必须等待承诺解决或拒绝,然后运行下面的其他代码。但是如果承诺拒绝我的代码停止并且不运行其他功能。我该如何解决?
await axios.all(promises).then(res => {
axios.patch("/url", { foo: bar }).then(async () => {
const promises2 = arr.map(item => {
return axios.post("/url-2", item)
});
await Promise.all(promises2)
.then(() => console.log("resolved")) //this not calling ever
.catch(() => console.log("failed")) //this not calling ever
console.log("This console log ever not working")
})
})
【问题讨论】:
-
你要么在
axios.all(promises)或axios.patch("/url", { foo: bar })被拒绝 - 所以我猜return axios.post("/url-2", item)也不会被执行 -
另外,你为什么在你的代码中使用 async/await,而不是在最后的 console.log 之前暂停......看起来你正在使用 async/await 而不是真的需要
-
也许您需要返回 await Promise.all(promises2) 并删除该命令中的 then/catch
标签: javascript reactjs promise