为什么我不能Error在catch回调中抛出一个内部并让进程处理错误,就好像它在任何其他范围内一样?
如果我什么都不做console.log(err)就会被打印出去,我对发生的事情一无所知.这个过程刚刚结束......
例:
function do1() {
return new Promise(function(resolve, reject) {
throw new Error('do1');
setTimeout(resolve, 1000)
});
}
function do2() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
reject(new Error('do2'));
}, 1000)
});
}
do1().then(do2).catch(function(err) {
//console.log(err.stack); // This is the only way to see the stack
throw err; // This does nothing
});
Run Code Online (Sandbox Code Playgroud)
如果回调在主线程中执行,为什么Error会被黑洞吞噬?
我们在Angular应用程序中有一个解决承诺的模式,直到Angular 1.6.0为止我们一直很好:
resource.get().$promise
.then(function (response) {
// do something with the response
}, function (error) {
// pass the error the the error service
return errorService.handleError(error);
});
Run Code Online (Sandbox Code Playgroud)
以下是我们如何在Karma中触发错误:
resourceMock.get = function () {
var deferred = $q.defer();
deferred.reject(error);
return { $promise: deferred.promise };
};
Run Code Online (Sandbox Code Playgroud)
现在,随着1.6.0的更新,Angular突然抱怨我们的单元测试(在Karma中)因"可能未处理的拒绝"错误而被拒绝的承诺.但是我们正在处理调用错误服务的第二个函数中的拒绝.
Angular究竟在寻找什么?它是如何让我们"处理"拒绝的?
javascript angularjs karma-runner angular-promise angularjs-1.6