4

I have the following JS code:

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

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

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)... Commented Oct 28, 2014 at 10:43

4 Answers 4

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

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

1 Comment

that is clear now..thanks very much :) yes i have AJAX calls. so i should run the second function as a response to the first one :)
2

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.com/blog/article/7-lines-JavaScript-library-for-calling-asynchronous-functions

http://jsbin.com/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

Comments

0

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 completed.

Comments

-2

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! :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.