The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Multiple HTTP Requests with $q

Multiple HTTP Requests with $q

5:06
In AngularJS, multiple HTTP requests can be coordinated with $q.all, making your project's code cleaner and more testable.
Watch this lesson now
Avatar
egghead.io

In AngularJS, multiple HTTP requests can be coordinated with $q.all, making your project's code cleaner and more testable.

Avatar
Andrew

The parsing of array elements by index in the callback is a bit groty. $q.all is not limited to consolidating an array of promises, it also works with an object containing promises. Thus, instead of

$q.all([ promiseA, promiseB, promiseC ]).then(function(result){
  var a = result[0],
        b = result[1],
        c = result[2];

  // process data using a, b, c
});

$q.all({ a: promiseA, b: promiseb, c: promiseC }).then(function(result){
  // promise data using result.a, result.b, result.c
});
In reply to egghead.io
Avatar
Joel

Nice Andrew, that is definitely a nicer approach in a lot of circumstances.

In reply to Andrew
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?