gpt4 book ai didi

javascript - clearTimeout 后出现 UnhandledPromiseRejectionWarning

转载 作者:行者123 更新时间:2023-11-30 19:20:51 24 4
gpt4 key购买 nike

<分区>

如果我在拒绝 promise 后清除计时器 - 无论我是否拦截 promise 的“捕获”,我都会在标准输出中收到警告“UnhandledPromiseRejectionWarning”

已检查: Node v10、 Node v12、google-chrome 76

let timer;
const promise = new Promise((resolve, reject) => {
timer = setTimeout(() => {
console.log('!timeout');
reject('timeout error');
});
}, 100);

promise.then(result => {
console.log('SUCCESS:', result);
}).catch(error => {
console.log('!onerror');
console.error('ERROR:', error);
});

promise.finally(() => {
console.log('!onfinnaly');
clearTimeout(timer);
});

console.log('!endcode');

nodejs 中的控制台输出:

!endcode
!timeout
!onfinnaly
!onerror
ERROR: timeout error
(node:12697) UnhandledPromiseRejectionWarning: timeout error
(node:12697) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:12697) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

google-chrome 中的控制台输出:

!endcode
!timeout
!onfinnaly
!onerror
ERROR: timeout error
Promise.catch (async)
Uncaught (in promise) timeout error

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com