如何解决发生未预期的诺言的意外的unhandledRejection事件
已更新,我现在已经尝试过explaining the behavior I'm seeing,但是从可信来源获得有关unhandledRejection
行为的答案仍然很棒。我还在Reddit上开始了discussion thread。
为什么在以下代码中出现unhandledRejection
事件(“错误f1”)?真是出乎意料,因为我在finally
的{{1}}部分处理了两个拒绝。
我在Node(v14.13.1)和Chrome(v86.0.4240.75)中看到了相同的行为:
main
解决方法
您应该使用try...catch
来捕获try
块内发生的所有错误:
try {
await p2;
// do something after p2 is settled
await p1;
// do something after p1 is settled
}
catch(e) {
// do something with errors e
}
编辑:
window.addEventListener("unhandledrejection",event => {
console.warn(`unhandledRejection: ${event.reason.message}`);
});
function delay(ms) {
return new Promise(r => setTimeout(r,ms));
}
async function f1() {
await delay(100);
throw new Error("error f1");
}
async function main() {
try {
const p1 = await f1();
await delay(200);
}
catch(e) {
console.warn(`caught inside main: ${e.message}`);
}
}
main().catch(e => console.warn(`caught on main: ${e.message}`));
,
好,对我自己说。我误解了unhandledrejection
事件的实际工作原理。
我来自.NET,那里有一个失败的Task
对象can remain unobserved,直到被垃圾回收为止。如果仍然无法执行任务,则只有UnobservedTaskException
被触发。
JavaScript承诺的情况有所不同。没有附加 的拒绝Promise
(通过then
,catch
,await
或Promise.all/race/allSettle/any
),需要尽可能早地使用一个 ,否则可能会触发unhandledrejection
事件 。
unhandledrejection
何时会被准确解雇?这似乎确实是特定于实现的。 W3C关于“未处理的承诺拒绝” do not strictly specify 的规范,当用户代理要通知已拒绝的承诺时。
为了安全起见,在当前函数将执行控制放弃给调用者之前,我会同步附加处理程序(通过类似return
,throw
,await
,{{ 1}})。
例如,以下命令不会触发yield
,因为unhandledrejection
承诺在已经创建await
后立即同步地附加到p1
拒绝状态。这是有道理的:
p1
即使我们异步地将window.addEventListener("unhandledrejection",event => {
console.warn(`unhandledRejection: ${event.reason.message}`);
});
async function main() {
const p1 = Promise.reject(new Error("Rejected!"));
await p1;
}
main().catch(e => console.warn(`caught on main: ${e.message}`));
处理程序附加到unhandledrejection
,以下内容仍然不会触发await
。我只能推测,这可能是发生的,因为已解决的承诺的续约发布为microtask:
p1
Node.js(发布时为v14.14.0)is consistent,具有浏览器行为。
现在,以下做触发window.addEventListener("unhandledrejection",event => {
console.warn(`unhandledRejection: ${event.reason.message}`);
});
async function main() {
const p1 = Promise.reject(new Error("Rejected!"));
await Promise.resolve();
await p1;
}
main().catch(e => console.warn(`caught on main: ${e.message}`));
事件。再次,我可以推测是因为unhandledrejection
延续处理程序现在异步地附加到await
上,并且在处理task (macrotask)队列时,在事件循环的某些后续迭代中:
p1
我个人觉得这整个行为令人困惑。我喜欢.NET方法来更好地观察window.addEventListener("unhandledrejection",event => {
console.warn(`unhandledRejection: ${event.reason.message}`);
});
async function main() {
const p1 = Promise.reject(new Error("Rejected!"));
await new Promise(r => setTimeout(r,0));
await p1;
}
main().catch(e => console.warn(`caught on main: ${e.message}`));
的结果。我可以想到许多情况,当我真的想要保留对诺言的引用,然后Task
并在以后的时间表中捕获到与解决或拒绝诺言有关的任何错误时。
也就是说,有一种简便的方法,可以在不引起await
事件的情况下获得本示例的预期行为:
unhandledrejection
,
我没有消息来源,但我认为它的工作方式如下: Promise.reject(new Error(“ Rejected!”));返回被拒绝的诺言,该诺言将在下一个滴答中出错。 所以:
async function main3() {
//this wil throw the error next tick
const p1 = Promise.reject(new Error("Rejected!"));
//this will run immediately and attach the await to the promise (so it will not be rejected)
await p1;
}
然后Promise.resolve将其结果返回给所有.then处理程序的下一个滴答(我们没有它们,因为不会存储结果) 所以:
async function main() {
//this wil throw the error next tick
const p1 = Promise.reject(new Error("Rejected!"));
//this will run immediately (and would give its value next tick)
await Promise.resolve();
//then this will run immediately and attach the await to the promise
await p1;
}
最后一个具有0个延迟的setTimeout不会立即触发,请检查:https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop并读取0延迟部分 所以:
async function main2() {
//this wil throw the error next tick
const p1 = Promise.reject(new Error("Rejected!"));
//setTimeout does with 0 does run not immediately.
//because of this the await p1 does not get added before the promise is rejected
await new Promise(r => setTimeout(r,0));
//so this does nothing and the prosime will reject
await p1;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。