"UnhandledPromiseRejectionWarning"即使存在捕获也出错 [英] "UnhandledPromiseRejectionWarning" error even when catch is present
问题描述
所以我有这段代码,createExampleDir
尚未实现,所以测试失败:
So I have this code, createExampleDir
is not yet implemented, so the test fails:
let chai = require('chai');
let expect = chai.expect;
let homeDir = require('home-dir');
let lib = require('../lib.js');
chai.use(require('chai-fs'));
let dir = homeDir('/example-dir');
describe('lib', () =>
describe('createExampleDir', () =>
it('should find ~/example-dir', () => lib.createExampleDir()
.then(() => expect(dir).to.be.a.directory())
.catch(() => {throw Error('Who cares!');})
)
)
);
问题是我遇到一个UnhandledPromiseRejectionWarning
错误,错误为谁在乎!"被抛出,所以抓到发生了:
The problem is that I'm getting an UnhandledPromiseRejectionWarning
error, the error "Who cares!" is throw, so the catch happens:
(节点:30870)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:2):[object Object] (节点:30870)[DEP0018] DeprecationWarning:已弃用未处理的承诺拒绝.将来,未处理的承诺拒绝将以非零退出代码终止Node.js进程.
(node:30870) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): [object Object] (node:30870) [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.
如何避免出现UnhandledPromiseRejectionWarning
警告?
What should I do to avoid the UnhandledPromiseRejectionWarning
warning?
推荐答案
在 chai.use
process.on('unhandledRejection', e => {
console.error(e);
});
您可以捕获错误和代码字符串号.
And you can catch error and code string number.
这篇关于"UnhandledPromiseRejectionWarning"即使存在捕获也出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!