AngularJS Multiple Identical API Calls

August 11, 2015
Reading Time: 1 minute

With a large AngularJS application, and even smaller ones, you can run into this issue where you end up with multiple identical API calls being made if your using the $q service. This occurs because there is a conflict with overlapping calls, which basically re-try the last API call for all of the call conflicts.

This happens when you aren’t bundling all of the calls up with the $q.all() function. This function receives an array of $http service objects that will resolve to an array once they’re all returned from the server.

You need to pass into the $q.all() function http promise objects which can be retrieved from the $http service.

For example:

Eliminating the .then after the $http and doing just¬† return $http(req);¬†will get you the promise to add to the array which you’ll pass to $q.all().

Leave a Reply

Your email address will not be published. Required fields are marked *