I am using $q in a geolocation service method, farther below.

The method makes promises to many dependents, leading to multiple calls, 
when, depending on the circumstances, only one might suffice. I naively 
added a clause ('THIS IS WHAT I WANT TO WORK') that would resolve any 
request with the last-found location, but that doesn't have the effect I 
hoped -- to block while the method is doing http and resolve when it is not.

What is the usual 'pattern' in angular for managing some async process like 
this in a central service, so that whenever the service is asked, it can 
decide whether or not to answer with a stored variable or a fresh http call?

    function getLocation(){ 

      return $q(function(resolve, reject) {

        if (lastLocationFound !== undefined && <OTHER CONDITION(S)>) { // 
THIS IS WHAT I WANT TO WORK
          resolve(lastLocationFound);
        }

        $http.get("https://api.ipify.org";).success(function(data){ // 
OBTAIN IP 

          var url = "https://freegeoip.net/json/"; + data + 
"?callback=JSON_CALLBACK"; 

          $http.jsonp(url). 
             success(function(data) { 

              var lat = parseFloat(data.latitude); 
              var long = parseFloat(data.longitude); 
              lastLocationFound = {lat: lat, long: long};  
              resolve({lat: lat, long: long}); 

           }).error(function(resp, code){ 
              // HANDLE 
           }); 
        }); 
      }); 
    }


Thanks,

G

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to