最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I use $q.all(promises) in angularjs - Stack Overflow

programmeradmin6浏览0评论

Usually I would do

$http({
    method:'GET',
    url: 'exmapleURL',
    params: {someParams}
}).then(function(response) {
    console.log(response); // response contains data I need
});

Now I have multiple such calls to run and I would like to wait for all of them to finish before doing something with their responses, and $q.all() seems to be a good place to start.

Per the AngularJS $q Service API Reference - $q.all document,

all(promises)

Combines multiple promises into a single promise that is resolved when all of the input promises are resolved.

Returns: Returns a single promise that will be resolved with an array/hash of values, each value corresponding to the promise at the same index/key in the promises array/hash. If any of the promises is resolved with a rejection, this resulting promise will be rejected with the same rejection value.

Does this mean that if I pass in an array of requests(like $q.all(calls).then(response)), the returned promises are in an array in the same order as the calls are passed in? Can I do something like response[0] to retrieve the response data returned by the 0th call? Thank you.

Usually I would do

$http({
    method:'GET',
    url: 'exmapleURL',
    params: {someParams}
}).then(function(response) {
    console.log(response); // response contains data I need
});

Now I have multiple such calls to run and I would like to wait for all of them to finish before doing something with their responses, and $q.all() seems to be a good place to start.

Per the AngularJS $q Service API Reference - $q.all document,

all(promises)

Combines multiple promises into a single promise that is resolved when all of the input promises are resolved.

Returns: Returns a single promise that will be resolved with an array/hash of values, each value corresponding to the promise at the same index/key in the promises array/hash. If any of the promises is resolved with a rejection, this resulting promise will be rejected with the same rejection value.

Does this mean that if I pass in an array of requests(like $q.all(calls).then(response)), the returned promises are in an array in the same order as the calls are passed in? Can I do something like response[0] to retrieve the response data returned by the 0th call? Thank you.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Oct 2, 2019 at 22:09 instant501instant501 3451 gold badge4 silver badges13 bronze badges 2
  • 2 Yes, you are correct. I'd suggest just giving it a try. – CollinD Commented Oct 2, 2019 at 22:11
  • Closely related: order in Promise.all – Bergi Commented Oct 2, 2019 at 23:08
Add a ment  | 

1 Answer 1

Reset to default 4

Does this mean that if I pass in an array of requests(like $q.all(calls).then(response)), the returned promises are in an array in the same order as the calls are passed in? Can I do something like response[0] to retrieve the response data returned by the 0th call?

Yes, the results in an array are returned in the same order:

angular.module("app",[])
.run(function($q) {
     var promise99 = $q.when(99);
     var promise22 = $q.when(22);
     
     $q.all([promise22,promise99]).then(function([result22,result99]) {
         console.log(result22);
         console.log(result99);
     });
});
<script src="//unpkg./angular/angular.js"></script>
<body ng-app="app"></body>

$q.all Also Accepts Hashes

angular.module("app",[])
.run(function($q) {
     var promise99 = $q.when(99);
     var promise22 = $q.when(22);

     var promiseHash = {
         r99: promise99,
         r22: promise22
     };
     
     $q.all(promiseHash).then(function(resultHash) {
         console.log(resultHash.r22);
         console.log(resultHash.r99);
     });
});
<script src="//unpkg./angular/angular.js"></script>
<body ng-app="app"></body>

For more information, see

  • AngularJS $q Service API Reference - $q.all
  • MDN JavaScript Reference - Destructuring Assignment
发布评论

评论列表(0)

  1. 暂无评论