Let's say depending on some condition your application dynamically fetches some script. Is that action synchronous and blocking, e.g. any ongoing rendering of HTML stops, or is it done asynchrounously?
1 Answer
The dynamically loading of the script would normally be asynchronous (via Ajax or a <script> tag). The execution of the script after it loads would be synchronous just like any other running script.
It is possible to run blocking Ajax calls to load it (though generally considered a bad idea), but I assume you're not doing that so using a normal Ajax call or a <script> tag would be async for loading.
4 Comments
wootscootinboogie
what i really meant was the evaluation of that javascript blocking and that, say, something like an ongoing animation would hang while the main thread evaluated the script.
jfriend00
@wootscootinboogie - a javascript-based animation would stall during the running of the script (but not during the loading of the script) because Javascript is single threaded so while one thread of execution is running, no timer events will run (which I assume is what is being used for a Javascript animation). CSS3 animations would be more likely to run since they don't have to wait for Javascript (though that may be implementation specific for each browser).
wootscootinboogie
this is probably getting too specific, but i know (or i think i now)that chrome has a browser thread that downloads and compiles the javascript. for browsers that do optimizations on the javascript. do you have any experience what happens in this situation?
jfriend00
@wootscootinboogie - That doesn't change anything. The download and compiling of the script is already async in all browsers (e.g. non-blocking), no matter how it's done.