1

In the following code:

function doStuffA() {
 // DO SOME THINGS

 doStuffB();

 // DO SOME MORE THINGS
}
function doStuffB() {
// DO B THINGS
}
doStuffA();

What order is the code executed?

Is it:

1. DO SOME THINGS
2. DO B THINGS
3. DO SOME MORE THINGS

Or:

1. DO SOME THINGS
2. DO B THINGS & DO SOME MORE THINGS - AT THE SAME TIME
13
  • 1
    You're not calling doStuffB inside A, you have a syntax error. Function calls do not use the function keyword. Commented Jan 24, 2013 at 21:02
  • 2
    This isn't real code and it won't run. Can you please show us something feasible? Commented Jan 24, 2013 at 21:03
  • 1
    Okay, syntax error is gone, but you still aren't calling doStuffA anywhere, so doStuffB will never run. Commented Jan 24, 2013 at 21:06
  • 1
    @bfavaretto this isn't real code. Is it more a theoretical question about how js executes functions. Hence the reason I don't actually have anything in the functions (just comments). Commented Jan 24, 2013 at 21:07
  • 3
    @AmandaMyer Next time, please post real code (or at least code that does something). Look of how much discussion and how many non-answers that triggered... Commented Jan 24, 2013 at 21:08

3 Answers 3

5

Assuming you intended for there to be a call to doStuffB in doStuffA, and a call to doStuffA somewhere...

Javascript is traditionally executed synchronously.

Therefore:

1. DO SOME THINGS
2. DO B THINGS
3. DO SOME MORE THINGS
Sign up to request clarification or add additional context in comments.

3 Comments

Was going to downvote this, but the question doesn't make sense. Nothing is actually called.
@NullUserException: I covered that in my other answer. ;) This one contains a disclaimer to the same effect, based upon the question title.
@NullUserException thanks for understanding! =) Will mark this as answer as soon as SO lets me.
2

There's been a lot of discussion about your syntax, but I think I can fill in the blanks well enough to understand what you're asking. You want to know whether JavaScript is executed sequentially; specifically, if a function call will pause execution of the calling function until the called function terminates.

General case

For the general case, the answer is yes. Here's some example code to illustrate this:

var count = 0;
var result = "";

function a() {
    result += "first part of A. \n";    
    b();    
    result += "last part of A. ";
}

function b() {
    for (var i = 0; i < 100000; i++) {
        count++;
    }
    result += "even after " + count + " cycles, A waits for B. \n";
}

a();
console.log(result);

Runnable version on jsFiddle: http://jsfiddle.net/jmorgan123/574Rh/

Try it out. You'll see that the result is:

first part of A. 
even after 100000 cycles, A waits for B. 
last part of A.

Exceptions to the rule

There are exceptions to this, and they're very important. When you set an interval or timeout, the program starts a timer and plans to run a function when the timer finishes. But it doesn't wait around for that to happen; once the timer has started, the rest of the program goes on with its other tasks:

//won't do what you want:
result += "first part of A ";  
setInterval(b, 1000);
result += "last part of A ";

In this case, when a() is finished running, result will be:

first part of A, last part of A 

In fact, that's all you'll see, because console.log(result) will run before b() does.

Another important case where JS defers execution is in AJAX calls; this trips people up all the time. Here's an example of this mistake:

//also won't do what you want:
result += "first part of A ";
$.get('ajax/test.html', function(data) {
    result += "now with AJAX! "; 
});
result += "last part of A ";

Again, result will be first part of A, last part of A when console.log(result) runs. The only way to guarantee your code runs after the AJAX call is by putting it in the callback:

var result = "";

function a() {
    result += "first part of A ";
    $.get('ajax/test.html', function(data) {
        result += "now with AJAX! "; 
        result += "last part of A ";
        console.log(result);
    });
}

a();

alert() and confirm()

One final note: Interestingly, the functions alert() and confirm() do, in fact, interrupt control flow. If your code looks like this:

result += "first part of A, ";  
result += confirm("what is your choice?"); //let's assume you click 'OK' here
result += ", last part of A";

...the result will be first part of A, true, last part of A no matter how long you wait to click OK. confirm and alert are the only cases (correct me if I'm wrong) where JavaScript will pause execution while it waits for some outside source.

2 Comments

Javascript never runs asynchronously. However, using timeouts you can defer the execution of certain blocks of code. They execute later, but still synchronously. That said, it's worth noting that the ECMAScript standard doesn't make any mandates in this regard either way.
@Non-StopTimeTravel - You're right, of course. I shouldn't have used synchronous/ansynchronous, I was just looking for a word to describe what I meant. I'll edit.
-1

Okay, assuming you want to call doStuffA()...

    function doStuffA() {
     // DO SOME THINGS

     doStuffB();

     // DO SOME MORE THINGS
    }
    function doStuffB() {
    // DO B THINGS
    }

The above will result in

1. DO SOME THINGS
2. DO B THINGS
3. DO SOME MORE THINGS

If you want the asynchronous approach you would use setTimeout(). Here are some docs: https://developer.mozilla.org/en-US/docs/DOM/window.setTimeout

i.e.

    function doStuffA() {
     // DO SOME THINGS

       setTimeout(doStuffB,1);

     // DO SOME MORE THINGS
    }
    function doStuffB() {
    // DO B THINGS
    }

4 Comments

@NullUserException - Another assume probably from the responder to the OP.
setTimeout decouples calls from the current flow, but it does not make them asynchronous in such a fashion. They are simply injected into a subsequent event handling "tick".
Yes people an assumption was made based on the context of the question. You will notice several other people made the same assumption. Most of your answers are simply pointing out the OPs lack of presentation. How about you try to help instead?
@Malk: "Helping" comes in many forms. Training someone to be precise is far more useful in the long run than anything else we could do here.

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.