title: AngularJS: Avoid calling same REST service twice before response is received
body: <p>I have two directives, each consuming the same factory wrapping a $q/$http call.</p>
  
  <pre><code>angular.module("demo").directive("itemA", ["restService", function(restService) {
      return {
          restrict: "A",
          link: function(scope, element, attrs) {
              restService.get().then(function(response) {
                  // whatever
              }, function(response) {
                 // whatever
              });
          }
      };
  }]);
  
  
  angular.module("demo").directive("itemB", ["restService", function(restService) {
      return {
          restrict: "A",
          link: function(scope, element, attrs) {
              restService.get().then(function(response) {
                  // whatever
              }, function(response) {
                 // whatever
              });
          }
      };
  }]);
  
  angular.module("demo").factory("restService", ["$http", "$q", function($http, $q) {
      return {
         get: function() {
             var dfd = $q.defer();
             $http.get("whatever.json", {
                 cache: true
             }).success(function(response) {
                // do some stuff here
                dfd.resolve(response);
             }).error(function(response) {
                // do some stuff here
                dfd.reject(response);
             });
         }
      };
  }]);
  </code></pre>
  
  <p>Problem:  When I do this</p>
  
  <pre><code>&lt;div item-a&gt;&lt;/div&gt;
  &lt;div item-b&gt;&lt;/div&gt;
  </code></pre>
  
  <p>I get the same web service fired off twice, because the GET from ItemA is still in progress when the GET for ItemB goes.</p>
  
  <p>Is there a way for whichever fires second to know that there's already a request to this in progress, so that it can wait a minute and grab it for free?</p>
  
  <p>I've thought about making an $http or $q wrapper which flags each URL as pending or not but I'm not sure that's the best way.  What would I do if it was pending?  Just return the existing promise and it'll resolve when the other resolves?</p>