NodeJS:当 Promise 在等待之前被拒绝时 UnhandledPromiseRejection
我在开始时有两个没有 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 reject
s before first promise resolve
s. So, while I'm still await
ing on the first promise, second promise is reject
ed 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 resolve
s?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在 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 withthen()
andcatch()
.