相关疑难解决方法(0)

我应该避免异步处理Promise拒绝吗?

我刚安装了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)

javascript node.js es6-promise

15
推荐指数
1
解决办法
9913
查看次数

标签 统计

es6-promise ×1

javascript ×1

node.js ×1