当我构建Promise并调用失败函数时,错误应该被Promise的.catch函数捕获,对吗?但是在console.log中,我仍然把它作为未捕获的错误(同时.catch函数也被触发)。为什么?这是有意的吗?我想我理解上出了些问题,希望得到启示!
考虑以下例子:
考虑以下例子:
var A = {
loadingPromise: null,
loadingPromiseFail: null,
loadingPromiseResolver: null,
init: function() {
this.loadingPromise = new Promise(
function(resolve, fail) {
this.loadingPromiseResolver = resolve;
this.loadingPromiseFail = fail;
}.bind(this)
);
this.loadingPromise.then(function(data) {
console.log('success');
}.bind(this));
this.loadingPromise['catch'](function(e, x) {
console.log('error', e);
}.bind(this));
},
doSomething: function() {
setTimeout(function(){
this.loadingPromiseFail('404');
}.bind(this), 1000);
}
}
A.init();
A.doSomething();
console.log:
error 404
uncaught exception: 404
为什么是第二个?