function (fe) { //this is actually more promise chain, I simplified for the question
return new Promise(function (resolve, reject) {
fe.getFile(fn, flags, resolve, function (e) {
if (e instanceof Error) {
reject(e);
} else {
reject(new Error(JSON.stringify(e)));//Lands here with file not found, says it's unhandled
}
});
}).catch(function(e){
//never comes here despite erroring, and never enters catch later in the promise chain
console.log(e);
});
}
我不确定为什么上面的代码在因找不到文件而被拒绝时会引发未处理的拒绝,那里有一个 catch 语句,然后在 promise 链中。
有任何想法吗?
这是有关文件条目的文档。
https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/index.html
我最好的猜测是插件 javascript 以某种方式违反了承诺?
第二个奇怪的部分是如果我像这样更改代码。
function (fe) { //this is actually more promise chain, I simplified for the question
return new Promise(function (resolve, reject) {
fe.getFile(fn, flags, resolve, reject); //pass it reject, instead of handling it in an anonymous function
});
}
Warning: a promise was rejected with a non-error: [object Object]
错误,但它正确地落入了陷阱并得到了正确的处理。
我不知道为什么添加匿名函数会导致这样的变化。