当某些调用有效而其他调用失败时 $q.all() 会发生什么?
我有以下代码:
var entityIdColumn = $scope.entityType.toLowerCase() + 'Id';
var requests = $scope.grid.data
.filter(function (rowData, i) {
return !angular.equals(rowData, $scope.grid.backup[i]);
})
.map(function (rowData, i) {
var entityId = rowData[entityIdColumn];
return $http.put('/api/' + $scope.entityType + '/' + entityId, rowData);
});
$q.all(requests).then(function (allResponses) {
//if all the requests succeeded, this will be called, and $q.all will get an
//array of all their responses.
console.log(allResponses[0].data);
}, function (error) {
//This will be called if $q.all finds any of the requests erroring.
var abc = error;
var def = 99;
});
当所有的 $http 调用都工作时,allResponses 数组就会填充数据。
当一个函数失败时,我的理解是将调用第二个函数并给出错误变量的详细信息。
但是,有人可以帮我解释一下如果某些响应有效而其他响应失败会发生什么情况吗?
最佳答案
我相信,由于 promise 库是基于 Q
实现的,一旦第一个 promise 被拒绝,就会调用 reject 回调并返回错误。它不会等待其他 promise 得到解决。请参阅 Q
的文档 https://github.com/kriskowal/q .对于Q.all这就是提到的
The all function returns a promise for an array of values. When this promise is fulfilled, the array contains the fulfillment values of the original promises, in the same order as those promises. If one of the given promises is rejected, the returned promise is immediately rejected, not waiting for the rest of the batch.
关于javascript - 当某些调用有效而其他调用失败时 $q.all() 会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19944922/