javascript - 当某些调用有效而其他调用失败时 $q.all() 会发生什么?

标签 javascript angularjs angular-promise

当某些调用有效而其他调用失败时 $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/

相关文章:

javascript - 寻找从字符串中提取未知子字符串的最简单方法。 (术语用斜杠分隔)

javascript - jQuery - $(window).load 与 $(window).prop ('complete' )

AngularJS ng-repeat 不适用于回调数据

ajax - 如何使用 ngresource post 返回 promise

javascript - 如何使用 $q.all 跨多个异步函数处理 $http.get 的结果

javascript - 文本区域中的选项卡导致 Opera 中失去焦点

javascript - Angularjs 从 1 个选择框中按过去 12 个月过滤

javascript - 在 Angular JS 中向 ng-click 添加参数

javascript - 如何使用 jquery 追加包含 Angular Directive(指令)的元素

javascript - AngularJS 等待 $resource 响应设置值