javascript: pass function as a parameter to another function, the code gets called in another order then i expect
<p>i want to pass a function to another function as a parameter.</p>

<p>I want to do that because the latter function calls an async Jquery method and AFTER that gives a result back, i want some javascript code executed.</p>

<p>And because this function is called from multiple places, i want the code to execute (after the async Jquery code gets executed) to be passed in the function.
Makes sense? i hope :)</p>

<p>Now what is see is that the order in which the code is executed is noth what i want.
I've simplified the code to this code:</p>

<pre><code>$('#AddThirdParty').click(function() {
    var func = new function() {
        alert('1');
        alert('2');
        alert('3');
    }
    alert('4');
    LoadHtml(func);
    alert('5');
});
function LoadHtml(funcToExecute) {
    //load some async content
    funcToExecute;
}
</code></pre>

<p>Now what i wanted to achieve (or at least what i thought would happen) was that alert4 would fire, then the loadhtml would fire alert1, alert2 and alert3, and then the code would return to alert5.</p>

<p>But what happens is this: alert1, alert2, alert3, alert4, alert5.</p>

<p>Does anyone know what i'm doing wrong and why this is the order in which the code is executed?</p>

<p>It looks like the alert1..alert3 gets executed when i define the <code>new function()</code>, but why doesn't it ALSO get executed when i call it from the <code>LoadHtml</code> function?</p>
