我终于得到了 Angular promise 错误处理,但这对我来说是违反直觉的。我预计错误将由失败回调处理,但我不得不使用 catch。
从概念上讲,我不太明白为什么要执行 catch 而不是失败回调。
我所期望的:
SomeAsyncService.getData().then(function (result) {
// The call is successful.
// Code in this block throws an error.
}, function (error) {
// I expected to handle errors here.
});
最终奏效了。
SomeAsyncService.getData().then(function (result) {
// The call is successful.
// Code in this block throws an error.
}).catch(function (error) {
// Where the error is actually caught.
});
如果有更合适的方法来处理 promise 错误,请告诉我。