title: handling ajax async calls
body: <p>Related to the following question.</p>
  
  <p><a href="https://stackoverflow.com/questions/14300700/empty-variables-when-calling-javascript-function">Empty variables when calling javascript function</a></p>
  
  <p>I'm seeking some guidance on how to ensure I get all results from ajax calls.</p>
  
  <p>In particular, this time the problem is with the <code>getPublicIPAddress()</code> function and its ajax call.</p>
  
  <pre><code>function genericYiiLocation(callback) {
  
  //console.log('genericYiiLocation: Creating location handler');
  
  this.longitude=  '';
  this.latitude=   '';
  this.accuracy=   '';
  this.publicIpAddress= '';
  
  var me = this;
  
  if (Modernizr.geolocation) {
      //console.log('genericYiiLocation: Location supported');
      if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(locateSuccess, locateFail);
  
  
      } else {
          alert('genericYiiLocation: Geolocation is not supported in your current browser.');
          callback(false);
      }
  } else {
      alert ('genericYiiLocation: no native location support');
      callback(false);
  }
  
  GetPoo();
  
  function GetPoo(getPublicIPAddress){
      console.log(this.accuracy);
  }
  
  
  
  
  function locateSuccess(loc){
      // console.log('genericYiiLocation: storing location data');
      me.longitude = loc.coords.longitude;
      me.latitude = loc.coords.latitude;
      me.accuracy = loc.coords.accuracy;
  
      callback(true, me);
  }
  
  // Unsuccessful geolocation
  function locateFail(geoPositionError) {
      switch (geoPositionError.code) {
          case 0: // UNKNOWN_ERROR
              alert('An unknown error occurred, sorry');
              break;
          case 1: // PERMISSION_DENIED
              alert('Permission to use Geolocation was denied');
              break;
          case 2: // POSITION_UNAVAILABLE
              alert('Couldn\'t find you...');
              break;
          case 3: // TIMEOUT
              alert('The Geolocation request took too long and timed out');
              break;
          default:
      }
      callback(false, geoPositionError);
  }
  
  function getPublicIPAddress(callback)
  {
  
      var ip;  
      $.ajax({
          type: 'GET',
          url: 'http://smart-ip.net/geoip-json?callback=?',
          dataType: 'json',
          async: true ,
          success: function(data) {
              ip = data.host;
              callback(false,ip);
  
          } 
      });
  
  
  //callback();        
  
  }
  
  
  }
  </code></pre>