title: Wait for $.ajax result inside .each function
body: <p>I have function that search for every element with a specific class:</p>
  
  <pre><code>$("#stepSurveyCtnId .questionCtnClass").each(function () {}
  </code></pre>
  
  <p>Inside each step, I check if a question is of type customer:</p>
  
  <pre><code>var type = $(this).children().data("question-type");
  
  var isCustomerQuestion = false;
  
  switch (type) {
      case "Name":
      case "Email":
          isCustomerQuestion = true;
          break;
  }
  </code></pre>
  
  <p>If it's customer type, I get the next id of the customer's table from the database:</p>
  
  <pre><code>  if(isCustomerQuestion) {
        if (customerId == -1) {
            $.ajax({
                method: "POST",
                url: urlCustomerCreate, 
                success: function (ajaxData) {
                    customerId = ajaxData.NumericValue;
                }
            });
        } 
    }
  </code></pre>
  
  <p>The issue is that in the second iteration of the .each() function, customerId is still = -1, when it should be 1305 for example.</p>
  
  <p>It seems that the execution don't stop in the $.ajax call, or the iterations are executed at the same time and the second iteration don't receive the customerId from the first iteration.</p>