我刚安装了Node v7.2.0并了解到以下代码:
var prm = Promise.reject(new Error('fail'));
Run Code Online (Sandbox Code Playgroud)
结果如下:
(node:4786) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: fail
(node:4786) 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.
Run Code Online (Sandbox Code Playgroud)
我理解这背后的原因,因为许多程序员可能已经经历了Error最终被一个人吞没的挫败感Promise.然而,我做了这个实验:
var prm = Promise.reject(new Error('fail'));
setTimeout(() => {
prm.catch((err) => {
console.log(err.message);
})
},
0)
Run Code Online (Sandbox Code Playgroud)
这导致:
(node:4860) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: fail
(node:4860) DeprecationWarning: Unhandled promise rejections …Run Code Online (Sandbox Code Playgroud)