NodeJS:当 Promise 在等待之前被拒绝时 UnhandledPromiseRejection

发布于 2025-01-16 21:27:43 字数 1330 浏览 1 评论 0原文

我在开始时有两个没有 catch 块的承诺,稍后我将使用 catch 块对这两个承诺进行等待。在第一个承诺解决之前,第二个承诺拒绝。因此,虽然我仍在等待第一个承诺,但第二个承诺已被拒绝,并且由于我尚未附加捕获,它会导致服务器因UnhandledPromiseRejection而崩溃。

有没有办法让 p2 的 reject 传播到 await p2,就像 p2 resolve 时那样?

PS:我无法在开头附加捕获。

添加以下代码以供参考。在 stackoverflow 中,由于某种原因它工作正常,但是当从本地运行时,服务器崩溃。

async function main() {
  console.log("starting");
  let p1 = new Promise((resolve, reject) => {
    console.log('p1 sleeping');
    setTimeout(function() {
      console.log('p1 awake');
      resolve('p1 resolved');
    }, 5000);
  });

  let p2 = new Promise((resolve, reject) => {
    console.log('p2 sleeping');
    setTimeout(function() {
      console.log('p2 awake');
      reject('p2 rejected');  //IMPORTANT
    }, 1000);
  });

  //Below line takes 5 seconds to execute.
  //But p2 throws error after 1 second because no .catch() is attached to it yet.
  let out1 = await p1.catch(e => {
    console.log('p1 errored', e)
  });
  console.log('p1 output', out1);

  let out2 = await p2.catch(e => {
    console.log('p2 errored', e)
  });
  console.log('p2 output', out2);
}

main().then(() => {
  console.log('stopped')
});

I have two promises without catch blocks in the beginning and I'm doing await on both of them later with catch blocks. Second promise rejects before first promise resolves. So, while I'm still awaiting on the first promise, second promise is rejected and since I have not attached the catch yet, it crashes the server with UnhandledPromiseRejection.

Is there a way to make p2's reject propagate to await p2, like it does in case when p2 resolves?

PS: I cannot attach the catch in the beginning itself.

Adding the code below for reference. In stackoverflow it works fine for some reason, but when run from local, the server crashes.

async function main() {
  console.log("starting");
  let p1 = new Promise((resolve, reject) => {
    console.log('p1 sleeping');
    setTimeout(function() {
      console.log('p1 awake');
      resolve('p1 resolved');
    }, 5000);
  });

  let p2 = new Promise((resolve, reject) => {
    console.log('p2 sleeping');
    setTimeout(function() {
      console.log('p2 awake');
      reject('p2 rejected');  //IMPORTANT
    }, 1000);
  });

  //Below line takes 5 seconds to execute.
  //But p2 throws error after 1 second because no .catch() is attached to it yet.
  let out1 = await p1.catch(e => {
    console.log('p1 errored', e)
  });
  console.log('p1 output', out1);

  let out2 = await p2.catch(e => {
    console.log('p2 errored', e)
  });
  console.log('p2 output', out2);
}

main().then(() => {
  console.log('stopped')
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

錯遇了你 2025-01-23 21:27:43

如果您在 Stack Overflow 控制台中运行代码,它仍然会抛出 UnhandledPromiseRejection ,因为您没有处理 Promise 拒绝(您可以在浏览器 DevTools 上看到错误)。

您应该尝试使用 Promise.all 或使用 then()catch() 处理每个 Promise 结果。

If you run your code in Stack Overflow console, it still throwns an UnhandledPromiseRejection because you are not handling the Promise rejection (you can se the error on your browser DevTools).

You should try using Promise.all or handle each Promise result with then() and catch().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文