0

I have next HTML code:

<!-- Virtual Router Section -->
<div id="virtualRouterSection" class="col-lg-12 col-xs-12">
    {% include "network-virtualRouter.html" %}
</div>

<!-- Zones Section -->
<div id="zonesSection" class="col-lg-12 col-xs-12">
    {% include "network-zones.html" %}
</div>

<!-- Profiles Section -->
<div id="profilesSection" class="col-lg-12 col-xs-12">
    {% include "network-profiles.html" %}
</div>

<!-- Ipsec Tunnel Section -->
<div id="ipsecTunnelSection" class="col-lg-12 col-xs-12">
    {% include "network-ipsecTunnel.html" %}
</div>

Each network-* HTML script has its own JS code as follows (network-virtualRouter.html example):

<script type="text/javascript">
    $.getScript("{% static 'network-virtualRouter.js' %}");
</script>

So the question is: How JS codes are interpreted? In mi mind, network-virtualRouter.js will be executed first. After that, network-zones.js, etc. Is this correct?

Thanks beforehand,

Mike.

2
  • [Yes it is correct. Of course if the js contain an async function, it will execute at the same time of the next one.] edit, see @T.J. Crowder answer instead Commented Feb 12, 2018 at 15:40
  • @Richard: Both halves of that comment are, unfortunately, incorrect. :-) Commented Feb 12, 2018 at 15:43

1 Answer 1

2

$.getScript is asynchronous. You're starting the process of grabbing the script in the order you see, but those requests may complete in any order at all (depending on the size of the resource, thread scheduling on the server, network vagaries, etc.). Whichever ones complete first will get run first, potentially not in the requested order.


In a comment you've asked:

However, I would like to execute the JS scripts in the order they are requested. How could i do that?

If I assume you want to make minimal changes (not switching to modules or something), then you could use a promise chain, either with native promises (or a polyfill for native promises) or jQuery's Deferred object.

With native promises: Make each call to $.getScript look like this:

var getScriptPromise = getScriptPromise || Promise.resolve();
getScriptPromise = getScriptPromise.then(function() {
    return $.getScript("{% static 'network-virtualRouter.js' %}");
});

With $.Deferred: Make each call to $.getScript look like this:

var getScriptPromise = getScriptPromise || $.Deferred().resolve().promise();
getScriptPromise = getScriptPromise.then(function() {
    return $.getScript("{% static 'network-virtualRouter.js' %}");
});

In both cases, we're using the semantics of var at global scope to either create the variable (if it's the first of these in the document) or use the existing variable (if not). If we created it, we create a fulfilled promise. If we didn't create it, we use the promise it already refers to. Then we use then to chain from it and make the next call to $.getScript. $.getScript returns a thenable, so returning it from then resolves the promise then returns to the thenable from $.getScript, meaning it waits for that thenable to settle and settles the same way.

End result: Each $.getScript call doesn't start until the previous one finishes.

If you want to have all the requests start at the same time, but then run the returned scripts in order, you'll have to use $.ajax instead to retrieve the script text, then perhaps $.globalEval to run it when you know it's that script's turn.

Sign up to request clarification or add additional context in comments.

2 Comments

Nice! That is what I wanted to know. However, I would like to execute the JS scripts in the order they are requested. How could i do that?
@Miguel.G: Added some notes on that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.