当某些调用有效而其他调用失败时 $q.all() 会发生什么? [英] What happens with $q.all() when some calls work and others fail?

查看:20
本文介绍了当某些调用有效而其他调用失败时 $q.all() 会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What happens with $q.all() when some calls work and others fail?

我有以下代码:

    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 数组将填充数据.

When all of the $http calls work then the allResponses array is filled with data.

当一个失败时,我的理解是将调用第二个函数并给出错误变量的详细信息.

When one fails the it's my understanding that the second function will be called and the error variable given details.

但是,有人可以帮我解释一下,如果某些响应有效而其他响应失败,会发生什么情况?

However can someone help explain to me what happens if some of the responses work and others fail?

推荐答案

我相信由于 promise 库是基于 Q 实现的,一旦第一个 promise 被拒绝,就会调用拒绝回调与错误.它不会等待其他承诺得到解决.请参阅 Q https://github.com/kriskowal/q 的文档.对于 Q.all 这就是所提到的

I believe since the promise library is based on Q implementation, as soon as the first promise gets rejected, the reject callback is called with the error. It does not wait for other promises to resolved. See documentation of Q https://github.com/kriskowal/q. For Q.all this is what is mentioned

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.

这篇关于当某些调用有效而其他调用失败时 $q.all() 会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆