【问题标题】:Promises can't run code after resolve/reject解决/拒绝后承诺无法运行代码
【发布时间】: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


【解决方案1】:

Promise 未正确链接,axios.patch(...) promise 未返回。 awaitthencatch 的语法糖,其目的是尽可能摆脱嵌套函数。应该是:

const res = await axios.all(promises)
await axios.patch("/url", { foo: bar })

const promises2 = arr.map(item => {
  return axios.post("/url-2", item)
});

try {
  await Promise.all(promises2)
  console.log("resolved"))
} catch (err) {    
  console.log("failed");
}

【讨论】:

    【解决方案2】:

    您的代码顺序错误。当然如果第一个 Promise 被拒绝,那么其余的都不会被调用。

    尝试以这种方式重写您的代码:

    let res = await axios.all(promises).catch(() => { console.log("failed"); return false; });
    
    if (!res) {
        // Do something when rejected
        ....
    }
    
    // Call the 2nd promise
    let res2 = await axios.path("/url", {foo: bar}).catch(() => {console.log("failed 2"); return false; });
    
    if (!res2) {
        // Do something when the 2nd promise is rejected
        ...
    }
    
    // Call your last promise
    let res3 = await Promise.all(promises2).catch(() => {console.log("failed 3"); return false; });
    
    if (!res3) {
        // Do something if it is rejected again
        ....
    }
    // Otherwise, do your thing
    

    【讨论】:

      【解决方案3】:

      试试这段代码,它应该能查明错误或拒绝发生的位置(即它肯定在Promise.all(promises2) 运行之前

      await axios.all(promises)
      .then(res => axios.patch("/url", { foo: bar }), err => {
          throw `all(promises) failed with ${err}`;
      })
      .then(() => {
          const promises2 = arr.map(item => {
              return axios.post("/url-2", item);
          });
          return Promise.all(promises2)
          .then(() => console.log("resolved")) //this not calling ever
          .catch(err => {
              throw `all(promises2) failed with ${err}`;
          });
      }, err => {
          throw `patch failed with ${err}`;
      })
      .catch(err => console.error(err));
      

      注意我已经删除了 async/await,因为在你发布的代码中它是完全没有必要的

      【讨论】:

      • 以这种方式与promises2部分不发送帖子(拒绝)和其他然后或Cath不工作
      • 你说的我一个都听不懂
      猜你喜欢
      • 2023-03-26
      • 2013-06-22
      • 1970-01-01
      • 2016-01-16
      • 2020-11-25
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      相关资源
      最近更新 更多