Javascript 未经处理的PromisejectionWarning,即使存在.catch on承诺
我有下面的问题,我想不出来。这是我的密码:Javascript 未经处理的PromisejectionWarning,即使存在.catch on承诺,javascript,node.js,error-handling,Javascript,Node.js,Error Handling,我有下面的问题,我想不出来。这是我的密码: //file1.js module.exports = { delete: function(id) { return new Promise((resolve,reject) => { setTimeout(() => {reject('bla bla');}, 2000); }); } } //file2.js const file1 = require('file1'); var de
//file1.js
module.exports = {
delete: function(id) {
return new Promise((resolve,reject) => {
setTimeout(() => {reject('bla bla');}, 2000);
});
}
}
//file2.js
const file1 = require('file1');
var delPr = file1.delete(id);
delPr.then(() => {
console.log('+++++++++++++++++++++++++++');
res.status(200).json({
message : "delete post"
});});
delPr.catch((error) => {
console.log('-----------------------------');
}
当这是我得到的代码时:
-----------------------------
(node:102437) UnhandledPromiseRejectionWarning: bla bla
(node:102437) 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: 1)
(node:102437) [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.
将file2.js中的代码更改为:
file1.delete(id).then(() => {
console.log('+++++++++++++++++++++++++++');
res.status(200).json({
message : "delete post"
});}).catch((error) => {
console.log('-----------------------------');
});
});
一切都很好,我只在控制台中得到了“-----------”,有人能解释一下区别吗??我没有看到,我已经用了3个小时了 是。然后()
返回一个承诺,而该承诺未被处理
因此,当您执行
a.then().catch()
时,该承诺的拒绝将由catch处理delPr的返回值。然后(…)
与delPr
不同,您可以详细说明吗???你说then
子句被命中,那么为什么我看不到++++
打印???我不是说实现回调被调用。我说,.then()
方法调用本身返回的不是delPr
,这就是您没有看到的区别。then()只在实现时调用,或者我错了?所有这些,在某个时候。您可能想阅读文档,了解在哪些情况下,返回的承诺,然后
/捕获
得到履行/拒绝。我不明白,拒绝
不是对原始承诺起作用吗??delPr
??如何解决承诺并输入然后
克隆,以及如何用捕获
捕获?