【发布时间】:2016-11-23 11:09:10
【问题描述】:
当我构建我的 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
为什么是第二个?
【问题讨论】:
标签: javascript promise