Q.Promise: 如何在回调中正确抛出异常?
Q.Promise: How to throw exception in callback correctly?
这里是我的简单代码http://jsfiddle.net/xh6960fo/
function test () {
var res = Q.defer();
res.resolve('Hello');
return res.promise;
};
test()
.then(
function(message) {
console.log(message);
throw new Error('Exception!');
},
function (err) {
console.log('no');
console.error(err);
})
.fin(function () {
console.log('fin');
});
我需要在 'then' 回调中引发异常。
但在控制台我只看到
Hello
fin
我没有引发异常。如何正确抛出异常?
then
的异常处理程序只捕获在它自己的成功处理程序之前发生的错误,不包括。
...
.then(function() {
throw new Error('error'):
})
.then(null, function(err) {
console.log(err);
})
.fin(...)
这里是我的简单代码http://jsfiddle.net/xh6960fo/
function test () {
var res = Q.defer();
res.resolve('Hello');
return res.promise;
};
test()
.then(
function(message) {
console.log(message);
throw new Error('Exception!');
},
function (err) {
console.log('no');
console.error(err);
})
.fin(function () {
console.log('fin');
});
我需要在 'then' 回调中引发异常。
但在控制台我只看到
Hello
fin
我没有引发异常。如何正确抛出异常?
then
的异常处理程序只捕获在它自己的成功处理程序之前发生的错误,不包括。
...
.then(function() {
throw new Error('error'):
})
.then(null, function(err) {
console.log(err);
})
.fin(...)