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]
错误,但它正确地落入捕获并正确处理。
我不知道为什么添加匿名函数会导致这样的改变。