title: Freebase: Asynchronous calls in a for loop
body: <p>I need to do n queries to <code>freebase</code> depending on the types an entity has. </p>
  
  <p>For example, if got the types in a previous query and I do:</p>
  
  <pre><code>for (var n=0;n&lt;types.length;n++) {
  subquery(types[n]);
  }   
  
  function subquery (typid) {
  query = [{
      "schema": {
          "id": typid,
          ... etc ... }
  }]
  
  $.getJSON(service_url + '?callback=?', params, function(response) {
      '... etc...';
  }
  }
  </code></pre>
  
  <p>But before the first <code>$.getJSON</code> responds, the second for loop is executed. I can't imagine the solution, although I read a lot of information about asynchronous calls.</p>