title: Javascript - Wait for function to return
body: <p>So, I am working on a project which requires me to call upon a function to get data from an external source.</p>
  
  <p>The issue I am having, I call upon the function - However the code after the function call is continuing before the function has returned a value.</p>
  
  <p>Here is the function -</p>
  
  <pre><code>function getData()
  {
      var myVar;
      var xmlLoc = $.get("http://ec.urbentom.co.uk/StudentAppData.xml", function(data) {
          $xml = $(data);
          myVar = $xml;
          console.log(myVar);
          console.log(String($xml));
          localStorage.setItem("Data", $xml);
          console.log(String(localStorage.getItem("Data")));
          return myVar;
      });
      return myVar;
      console.log("Does this continue");
  }
  </code></pre>
  
  <p>And here is where it is called upon - </p>
  
  <pre><code>$(document).on("pageshow","#Information",function() {
      $xml = $(getData()); //Here is the function call
      console.log($xml); //However, it will instantly go to this line before 'getData' has returned a value.
      $xml.find('AllData').each(function() {
          $(this).find('item').each(function() {
              if ($(this).find('Category').text()=="Facilities") {
                  console.log($(this).find('Title').text());
                  //Do stuff here
              } else if ($(this).find('Category').text()=="Contacts" || $(this).find('Category').text()=="Information") {
                  console.log($(this).find('Title').text());
                  //Do stuff here too
              }
          });
          $('#informationList').html(output).listview().listview("refresh");
          console.log("Finished");
      });
  });
  </code></pre>
  
  <p>Right now, I'm unsure of why it is not working. My guess is that it is because I am calling a function within a function.</p>
  
  <p>Does anyone have any ideas on how this issue can be fixed?</p>