强制拒绝 Angular $http 调用
Force rejecting Angular $http call
我正在使用 $http
拨打电话。基于调用的成功结果,我可能会决定抛出一个 error/reject 并将其作为错误传递到下一个调用。但是,如果抛出错误,它只会停止该过程。我如何强制拒绝 $http 承诺而不将其包装在某些 $q 代码中?
// A service
angular.module('app').factory('aService', function ($http, config) {
return {
subscribe: function (params) {
return $http({
url: '...'
method: 'JSONP'
}).then(function (res) {
// This is a successful http call but may be a failure as far as I am concerned so I want the calling code to treat it so.
if (res.data.result === 'error') throw new Error('Big Errror')
}, function (err) {
return err
})
}
}
})
// Controller
aService.subscribe({
'email': '...'
}).then(function (result) {
}, function (result) {
// I want this to be the Big Error message. How do I get here from the success call above?
})
在上面的代码中,我希望大错误消息以拒绝调用结束。但是在这种情况下,它只是因错误而死。这就是我在 say Bluebird 中处理事情的方式,但这里不行。
Ti 在拒绝状态下继续链只是 return 一个被拒绝的承诺 $q.reject('reason')
来自你的 $http 结果类似于
$http.get(url).then(
function (response){
if(something){
return $q.reject('reason');
}
return response;
}
)
这样你就会得到一个被拒绝的承诺,即使 api 调用成功,你也可以对其做出反应。
我正在使用 $http
拨打电话。基于调用的成功结果,我可能会决定抛出一个 error/reject 并将其作为错误传递到下一个调用。但是,如果抛出错误,它只会停止该过程。我如何强制拒绝 $http 承诺而不将其包装在某些 $q 代码中?
// A service
angular.module('app').factory('aService', function ($http, config) {
return {
subscribe: function (params) {
return $http({
url: '...'
method: 'JSONP'
}).then(function (res) {
// This is a successful http call but may be a failure as far as I am concerned so I want the calling code to treat it so.
if (res.data.result === 'error') throw new Error('Big Errror')
}, function (err) {
return err
})
}
}
})
// Controller
aService.subscribe({
'email': '...'
}).then(function (result) {
}, function (result) {
// I want this to be the Big Error message. How do I get here from the success call above?
})
在上面的代码中,我希望大错误消息以拒绝调用结束。但是在这种情况下,它只是因错误而死。这就是我在 say Bluebird 中处理事情的方式,但这里不行。
Ti 在拒绝状态下继续链只是 return 一个被拒绝的承诺 $q.reject('reason')
来自你的 $http 结果类似于
$http.get(url).then(
function (response){
if(something){
return $q.reject('reason');
}
return response;
}
)
这样你就会得到一个被拒绝的承诺,即使 api 调用成功,你也可以对其做出反应。