是否有办法触发$ q.all,无论承诺是否返回错误?
我正在尝试执行多个$ http.post请求,从用户输入的文本字段中发布值。后端(Django REST框架)有一个我们实现的值检查器,所以如果POSTed的值与预期的不同(即,提交一个字符串,其中一个整数是预期的),将返回400状态,这反过来导致$ q.all不会触发,这会导致我的应用程序出现许多不同的错误。
//Beginning of for loop, getting values in text fields, setting
//up other things I'm not sure are really relevant here.
var writeRes = $http({
method: 'POST',
url: '/' + api_prefix + '/values/',
data: out_data, //defined elsewhere, not relevent here.
headers: { 'Content-Type': 'application/json','X-CSRFToken': $scope.valueForm.csrf_token,'Accept': 'application/json;data=verbose' } // set the headers so angular passing info as form data (not request payload)
});
saved.push(writeRes);
//array defined at beginning of for loop
writeRes.success(function(data, status, headers, config){
$scope.scope.param_values.push(data.id);
//array of IDs relevant to the REST framework.
});
error(function(status){
//not sure what to do here.
});
}
}
$q.all(saved).then(function() { //perform other PATCH, DELETE, GET tasks }
正在发布正确的值,但如果出现错误,则$ q.all中的后期处理不会被触发,这会在页面刷新时导致很多问题。
有没有办法可以触发$ q.all而不管错误?
如果我的问题看起来难以捉摸,我很抱歉,我对前端开发并不太了解,并且觉得我正在与这个项目一起运行。
答案 0 :(得分:1)
documentation for the $q.reject()
method显示了你如何能够抓住"拒绝回调并从中恢复。
在这种情况下,您只需从拒绝功能返回一个新值,$q
会将承诺视为已解决:
writeRes.then(function(data, status, headers, config){
$scope.scope.param_values.push(data.id);
//array of IDs relevant to the REST framework.
}, function(error){
// handle the error and recover
return true;
});