Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

angularjs

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 ?

like image 461
Samantha J T Star Avatar asked Nov 06 '14 10:11

Samantha J T Star


Video Answer


1 Answers

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];
});
like image 137
Austin Greco Avatar answered Oct 10 '22 11:10

Austin Greco