title: Pausing a for each loop while waiting for ajax.done to complete
body: <p>I am iterating through an array of command files using the for each method.</p>
  
  <p>With each command file, I need to wait for ajax success before continuing on with the next command.</p>
  
  <p>The problem is that the for each loop moves on to the next command before the ajax code has finished. Can anyone offer a solution?</p>
  
  <p>For each loop:</p>
  
  <pre><code>$.each(cmd_files, function(index, cmd) {
  
            update_log('Running CMD for' + cmd)
  
            wait_for_cmd_complete(cmd).done(function(data){
  
                 update_log("CMD is complete");    
  
            })
   })
  </code></pre>
  
  <p>Ajax function:</p>
  
  <pre><code>function wait_for_cmd_complete(cmd){
  
       return $.ajax({
           type: 'POST',
           data: {cmd:cmd}, 
           url: 'wait_for_cmd_complete.php'
       });  
  
  
    }
  </code></pre>