title: Javascript callback functions with ajax
body: <p>I am writing a generic function that will be reused in multiple places in my script.</p>
  
  <p>The function uses ajax (using jQuery library) so I want to somehow pass in a function (or lines of code) into this function to execute when ajax is complete.
  I believe this needs to be a callback function, but after reading through a few callback answers I'm still a bit confused about how I would implement in my case.</p>
  
  <p>My current function is:</p>
  
  <pre><code>function getNewENumber(parentENumber){
  
          $.ajax({
                 type: "POST",
                 url: "get_new_e_number.php",
                 data: {project_number: projectNumber, parent_number: parentENumber},
                 success: function(returnValue){
                      console.log(returnValue);
                      return returnValue; //with return value excecute code
  
                  },
                  error: function(request,error) {
                      alert('An error occurred attempting to get new e-number');
                      // console.log(request, error);
                  }
          });
      }
  </code></pre>
  
  <p>With this function I want to be able to do something in the same way other jQuery functions work ie;</p>
  
  <pre><code>var parentENumber = E1-3;
  
  getNewENumber(parentENumber, function(){
      alert(//the number that is returned by getNewENumber);
  });
  </code></pre>