title: return value from function with XMLhttprequest
body: <p>I am very new to JS, trying to create simple page which does next:</p>
  
  <ol>
  <li>takes IP of some server</li>
  <li>then sends a get request to this server</li>
  <li>parses get response, </li>
  <li>adds filtered lines to the table on html page.</li>
  </ol>
  
  <p>I was able to do all the steps through the browser console but when, moving to the JS file with get function for some reason function does not return value.</p>
  
  <p>In below code snip line 6 will print undefined in the console. 
  Any idea how to return "statuses" from the function getStatus? 
  Should it be some timeout between line 5 and 6? </p>
  
  <p>Thanks!</p>
  
  <pre><code>$("input[type='text']").keypress(function(event){
     if(event.which === 13){
      var address = $(this).val();
      var urlStat = 'http://'+address+':666/bla?open=stats';
      var status = getStatus(urlStat);
      console.log(status);
  
      $("input[type='text']").val('');
      $('table').append("&lt;tr&gt;&lt;th&gt;"+address+"&lt;/th&gt;&lt;th&gt;&lt;ul&gt;&lt;/ul&gt;&lt;/th&gt;&lt;th&gt;&lt;/th&gt;&lt;/tr&gt;");
  }
  });
  
  function getStatus(url){
  var xhr = new XMLHttpRequest;
  
  xhr.open("GET", url);
  xhr.send();
  xhr.onreadystatechange = function() {
      if (this.readyState == 4 &amp;&amp; this.status == 200) {
  
      var regexStatus = /(\w+ state:.*?)&lt;/g
      var response = xhr.responseText;
      var statuses = response.match(regexStatus);
      console.log('Inside function getStatus'+statuses);
      return statuses;
      };
  }
  };
  </code></pre>