title: Mixing sync and async javascript/jquery and getting a success function at the end
body: <p>Wondering what the best solution to this problem is, also this is not my actual code structure or names but the simplest way to illustrate the problem. 
  I have a function which was purely used to perform an ajax call and load a template with jquery.</p>
  
  <pre><code>function load(template) {
      $('#container').load(template, data, function() {
         // complete code here
      }); 
  }
  </code></pre>
  
  <p>Focusing on the 3rd param in $.load(), namely a callback function that runs when the request is complete.</p>
  
  <p>Now I have my load() function in another wrapper function:</p>
  
  <pre><code>function processTask(variable) {
      load(variable);
  }
  </code></pre>
  
  <p>The problem I have is I need some code to run after the ajax load is complete, however as my app has grown my wrapper function <code>processTask</code> may or may not invoke an ajax <code>load</code> so I can't perform my must needed code inside the complete callback.</p>
  
  <p>Do I change my $.load() to perform synchronous or just manage my code better so that if I am calling a $.load() it puts my needed code in the callback and if not it places it where I need it to be?</p>
  
  <p>I have read about javascript Promises and I'm unsure if they will help in this situation.</p>
  
  <p>EDIT</p>
  
  <p>So my <code>processTask</code> is an object method.</p>
  
  <pre><code>function classObj(name, fn) {
      this.name = name;
      this.processTask = fn;
      this.load = function(template) {
          $('#container').load(template, data, function() {
             // complete code here
          });
      }
  }
  </code></pre>
  
  <p>And in context I do this:</p>
  
  <pre><code>var task = new classObj('taskName', function() {  
      this.load('myFile.php'); 
      // Or another function and not load() based on whats needed in the task. 
  });
  </code></pre>
  
  <p>Basically I have an object that I can add custom methods to at will and they can easily be called dynamically, until now they have always loaded a file.</p>