R
R
Roman Nazarkin2017-01-10 18:09:42
JavaScript
Roman Nazarkin, 2017-01-10 18:09:42

Is there a sequence of calls to self-executing functions in JS?

Good afternoon!
There is a similar piece of code:

(function ($) {

    // build new menu markup
    var $fn = $('<div />', {id: 'our-unique-id'})
        .wrapInner('<div class="inner-wrap"><nav/></div>');

    // .. do something with it ..

    $fn.appendTo($('body'));

})(jQuery);

(function ($) {

    var $el = $('#our-unique-id'); // А НАЙДЕТ ЛИ?

})(jQuery);

That is, the essence is that we create an element inside one function, place it at the end of the body (without any callbacks), then inside the second we already refer to this element. The question is - will it work?
Given the asynchrony of JS, I can assume that both functions will run simultaneously in some situation and the second one will request the element when the first one has not yet physically created it.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Makarov, 2017-01-10
@TrickyMilk

It will be useful to read about EventLoop in JS. (you can watch the part of the cantor's podcast about nodeJS, it is very well covered there)
Asynchronous functions are queued, where they are executed when the interpreter is free.
But in your case, the functions are synchronous, so it should execute

T
Tsimur_S, 2017-01-10
@Tsimur_S

>> Given the asynchrony of JS, I can assume that both functions will run simultaneously in some situation and the second one will request the element when the first one has not yet physically created it.
Where do you have asynchrony in the code of the first IIEFE?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question