Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does jQuery's synchronous AJAX request work?
    text
    copied!<p>Does <code>jQuery.ajax({async: false})</code> still use the XMLHttpRequest object?</p> <p>If so, how is the request synchronous?</p> <p>Is there just some mechanism inside the function definition that allows the asynchronous request put out by the XHR to be wrapped in a synchronous wrapper?</p> <p>I ask because I want to wrap asynchronous function calls into a synchronous wrapper.</p> <p><strong>EDIT</strong>:</p> <p>I want to do this for dependencies. I need to not run anything more until all the external dependency scripts are loaded, however, I'd rather not load each of the files synchronously.</p> <p>Basically, I want this:</p> <pre><code>require(['lib1.js','lib2.js']) Library1Function(); Library2Function(); </code></pre> <p>To load lib1 and lib2 at the same time, but block until both are loaded.</p> <p>I know I could do this with a callback, however that doesn't work if the files I'm including also have dependencies included in the same way.</p> <p><strong>EDITx2</strong></p> <p>Reason why callbacks don't work:</p> <pre><code># lib2.js window.Library2Function = function(input) { alert(input); } # lib1.js require('lib2.js', function() { window.Library1Function = function() { window.Library2Function('Hi there'); } }); # main.js require('lib1.js', function() { window.Library1Function(); }); </code></pre> <p>The problem is that, in main.js, that callback will get sent off once lib1.js is loaded and run. The problem is, <code>Library2Function</code> isn't actually defined until lib2.js is loaded, which happens <em>after</em> lib1.js is parsed.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload