Are javascript functions synchronous or async? - Stack Overflow

I have the following JS code:<script>first();second();<script>I want to make sure that

I have the following JS code:

<script>
first();
second();
</script>

I want to make sure that second() will run after plete execution of first(). Is that the expected default behavior?

I have the following JS code:

<script>
first();
second();
</script>

I want to make sure that second() will run after plete execution of first(). Is that the expected default behavior?

Share Improve this question asked Oct 28, 2014 at 10:38 Michael SamuelMichael Samuel 3,92012 gold badges51 silver badges86 bronze badges 1
  • Isn't faster to try and see (or well...check JS specs? if it wasn't like that it's a big difference with 99.99% languages out there then well highlighted in doc)... – Adriano Repetti Commented Oct 28, 2014 at 10:43
Add a ment  | 

4 Answers 4

Reset to default 5

It depends on what you have inside your first() and second() functions.. if you have some async calls, first() may end after second().

For example

function first(){
    console.log("I'm first");   
}
function second(){
    console.log("I'm second");   
}

first();
second();

will print

I'm first

I'm second

Now suppose you have an ajax call in your first() function that takes 10 seconds to end:

function first(){
    $.ajax({
        //--- blah blah
        success: function(){
            //--- success runs after 10 seconds
            console.log("I'm first");   
        }
    })
}

if you run

first();
second();

you will have printed

I'm second

I'm first

Here you can find another example

Yes it is expected behavior. You can define also some asynchronous functions like AJAX calls. You can also define behavior that is similar to asynchronous calls check this links

http://krasimirtsonev./blog/article/7-lines-JavaScript-library-for-calling-asynchronous-functions

http://jsbin./AhirAlOV/5/edit?html,js,output

Important:

Also remember that JavaScript is not multithreaded language. JavaScript will run in a single thread, but will be executed in blocks. So it will have to finish each block of code it has queued up before continuing to the next block. You can get illusion of asynchronous calls with events and callbacks

Most functions in Javascript are synchronous. If you were to call several synchronous functions in a row

 first();
 second();     

they will execute in order. second will not start until first has pleted.

Javascript is an Asynchronous language. The reason why they call it an asynchronous language is cause all the functions execute on an event basis, with the help of event handlers and we really can't be sure when the events will fire. It can be a mouse click event, load event, etc. However the execution of functions happen sequentially. Only after the first function execute will the second start. But keep in mind that Javascript is an Asynchronous language and why it is called so. So to answer your question, yes! :)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745143858a4613555.html

相关推荐

  • Are javascript functions synchronous or async? - Stack Overflow

    I have the following JS code:<script>first();second();<script>I want to make sure that

    10小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信