title: Return data from Closure Within Block Within Function
body: <p>I have setup my JavaScript thus</p>
  
  <pre><code>function getData(uri) {
      var $result;
      var demoAPI = uri;
      $.getJSON(demoAPI, function(data) {
          $.each(data, function(key, val) {
              names[key] = val["name"];
              numbers.push(val["number"]);
          });
      }).done(function() {
              for(i = 0; i &lt; names.length; i++) {
                  var tmp = new Array(names[i], numbers[i]);
                  result.push(tmp);
              }
              return result;
          });
  }
  </code></pre>
  
  <p>Problem is, with this setup is I cannot use the function thus:</p>
  
  <pre><code>var values = getData('/users');
  </code></pre>
  
  <p>as the <code>return</code> statement is in a closure within the <code>$.getJSON</code> block, if I do call the <code>return</code> after said block it returns an empty array because it gets called before the ajax call is completed. I will encounter the same challenge if I attempt to use <code>$.ajax()</code>. I need help returning the generated array from my function <code>getData()</code>.</p>