<分区>
如果我在拒绝 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