title: JQuery how to wait with return untill getJSON is done? (Web api)
body: <p>I'm making a project that includes web api and JQuery. One of the html pages has to show store and the manager's name of the store.</p>
  
  <p>Every store has:<br>
  ID,<br>
  StoreName,<br>
  ManagerID.</p>
  
  <p>Every Manager has:<br>
  ID,<br>
  ManagerName.</p>
  
  <p>The store isn't hold the manager name directly.</p>
  
  <p>The code looks like this:</p>
  
  <pre><code>// There is a html code with &lt;table id='table'&gt;...&lt;/table&gt; before the script.
  var storesUri = 'api/stores' //The stores uri
  $(document).ready(function() {
      $.getJSON(storesUri)
          .done(function(data) {
              $.each(data, function(key, item) {
                  $('#table).append(
                      '&lt;tr&gt;' +
                      '&lt;td&gt;' + item.ID + '&lt;/td&gt;' +
                      '&lt;td&gt;' + item.storeName + '&lt;/td&gt;' +
                      '&lt;td&gt;' + findManagerNameByID(item.ManagerID) + '&lt;/td&gt;' + //get the manager name by the manager id
                      '&lt;/tr&gt;');
              });
          });
  });
  
  var managersUri = 'api/managers' //The managers uri
  function findManagerNameByID(id) {
      var res = 'N/A';
      $getJSON(managersUri + '/' + id)
          .done(function(data) {
              res = data.ManagerName; //puts in res the fit manager name
          })
          .fail(function(jqXHR, testStatus, err) {
              res = 'None';
          });
      //alert('1');
      return res;
  }    
  </code></pre>
  
  <p>After this code the table has to include for each store the store ID, store name and the store manager name. Instead of that the table includes for each store the store ID, the store name and 'N/A' (the default value of res).</p>
  
  <p>The thing is that, if I add to the code "alert('1');" right before the return (You can see it in commant) the table will be okay and it'll include for each store the store ID, store name and the store manager as I wanted.</p>
  
  <p>I think this is happen becase the 'return res;' line somehow happens before the 'res=data.ManagerName;' line (I think that because when the code has the little delay of the alert before the return, everything is okay).
  How can I make the table be filled correctly without the alert?
  thanks.</p>