title: AngularJS getting data from service
body: <p>I have a service method for getting some data. The service method will first check the local storage for the presence of that data, if present then it will return else it will fetch the data from server using $resource and return  after storing it in local storage. From my controller I will call this service to get the data. My question is, how to make sure that data is returned after the ajax call is over. when I call the below service from my controller the first time I get <code>undefined</code></p>
  
  <pre><code>services.factory('dataService', ['country','localStorageService',function(country,localStorageService) {
   return { 
  
      getCountryList: function (){
                       var countryList = null;
                       if(localStorageService.isSupported) {
                              countryList = localStorageService.get("countryList");
                       }
                       if(countryList !==null){
                          return countryList ;
                       }
                       else{
                          country.query(function(response) {
                              countryList = response;
                              if(localStorageService.isSupported) {
                                  localStorageService.set("countryList ",countryList );
                              }
                              return countryList ;
                          });
                       }
  
  
  
                   }
      }
  }]);
  
  
  services.factory('country',['$resource','serverConfig',function($resource,serverConfig) {
       return $resource(serverConfig.url+'/country');
  }]);
  </code></pre>