title: Waiting for function to finish - execution is asynchronous (not in order)
body: <p>I'm trying to get user's city and country before moving on with my code. It seems as if javascript is not executed in the order I need.</p>
  
  <pre><code>$(document).ready(function() {    
    var country, city = '';
  
    function geoData() {
      $.getJSON('http://ipinfo.io/json?callback=?', function (data) { 
        console.log('step 1');
        country = data.country;
        city = data.city;
        console.log('step 2');
      });
    };
  
    geoData();        
    console.log('step 3');
  
    /* rest of the code */
  });
  </code></pre>
  
  <p>I want the code to be executed as:</p>
  
  <pre><code>step 1
  step 2
  step 3
  </code></pre>
  
  <p>However when I run the script I get:</p>
  
  <pre><code>step 3
  step 1
  step 2
  </code></pre>
  
  <p>Why is the code running in an asynchronous way? Any suggestions how I can fix it?</p>
  
  <p>Thanks.</p>