Samantha J T Star
Samantha J T Star

Reputation: 32828

How can I use the AngularJS hash option of $q.all?

I have looked around and found nothing at all to explain how to use this. The documentation states:

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.

But there is no example.

Does anyone have any examples of using the key / hash method ?

Upvotes: 7

Views: 1179

Answers (1)

Austin Greco
Austin Greco

Reputation: 33554

There don't seem to be many examples of this, but it should work like this:

// as an object
$q.all({
  one: $http.get('/url1'),
  two: $http.get('/url2')
}).then(function (results) {
  var data1 = results.one;
  var data2 = results.two;
});

// as an array
$q.all([
  $http.get('/url1'),
  $http.get('/url2')
]).then(function (results) {
  var data1 = results[0];
  var data2 = results[1];
});

Upvotes: 12

Related Questions