title: Jquery Ajax call with promise
body: <p>I have a form with validation code that requires an Ajax call to a database, the return value from which must be handled before the form is submitted. I've tried using promises as below but am obviously misundersanding the syntax. The return values are still in the required order. Your help will be greatly valued.</p>
  
  <p>The key parts of the code are:</p>
  
  <pre><code>// checks form submission
  $('#ml_doaction').on('click', function(e) {
    e.preventDefault();
    var isOK = true;
    $('form#managelists :input').each(function(){
      var id = this.id;
      switch(id)
      {
        case 'maillist':
          checkNewListName(id).then(function(data) {
          if(data === false) {
            isOK = false;
          }
        });
        break;
  
        // etc ....
        }
    });
  
    if(isOK === true) {
      // submit form
      $('form#managelists').submit();
    }
  });
  
  function checkNewListName(id)
  {
    var listVal = $('#' + id).val();
    var dfd = $.Deferred();
    $.ajax({
      type: 'POST', 
      data:{'checklistexists': listVal},
      url:'/php/ajax/validation-ajax.php',
      success:function(response){
        if(response == 'listexists') {
          dfd.resolve(false);
        }
        else {
          dfd.resolve(true);
        }
      }
    });
    return dfd.promise();
  }
  </code></pre>