0

I'm working through loading javascript dependencies and I'm trying to answer a question about how getScript works.

I've built loading code that will automatically load scripts using getScript, basically by looping through an array of filepaths and calling getScript on each one.

Since getScript runs asynchronously, I had expected no guarantee which file would finish loading first and, presumably, begin executing in the main thread.

However, it appears that if I make multiple calls to getScript, the calls are queued internally, such that later files do not begin loading until the earlier files have completed. That is, my main function certainly runs asynchronously to getScript, but even if I load a very large file first (and a small file last), the large file always executes first.

Given that the order of execution never changes up (even when I load a very large file before a smaller one), I'm left to assume that while getScript executes asynchronously with the main execution thread, it does not execute asynchronously with itself...?

I hope that makes enough sense that someone can clarify this for me...

Joel Graff
  • 167
  • 15
  • No, there is no queue (or no guarantee for it, at least). Your browser might queue the requests internally, but other browsers may not - the order of execution is unpredictable. Do not rely on this behaviour. – Bergi Feb 24 '14 at 21:49
  • it depends on how and when the external script is added. if it is added during load via document.write(), you can depend on the order. if it uses appendChild() the browser and ISP can cause re-ordering. AppendChild is more common. If your tool leaves a staircase pattern on your console's network tab, you can do better. – dandavis Feb 24 '14 at 22:13

0 Answers0