2
function method1() {
    method2();
    var x = 1;
}

function method2() {
    $.ajax({
        type: "GET",
        async: true,
        url: "/MyController/MyMethod",
        success: function (data) {
            var y = 6;
        }
    });
}

Which happens first - the initialization of y or the initialization of x? Which would be the order of these two lines if the ajax call was sync instead of async?

method1 is called in the loading of the page.

3
  • Well, it depends which method you call, since the variables only exist inside the functions Commented Oct 17, 2012 at 12:22
  • If the call is synchronous, it depends on where the GET request is successful or not. Commented Oct 17, 2012 at 12:24
  • @Thilo Let's assume it always enters in success. Commented Oct 17, 2012 at 12:25

2 Answers 2

2

If it's synchronous and if the GET is successful then Y is initialized first. If it's asynchronous it could be either, but I'd put my money on X simply because I'd suspect it'd go on before the web method returns. That being said there's no assurance that X will be fired first.

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

3 Comments

"If it's asynchronous it could be either, " No. Since Javascript is single-threaded, the current code path will complete before any callback are executed. X will always be fired first.
@Thilo Can you explain a bit further?
In Javascript, only one code path runs at the same time. If you call a function (such as method1) it will run to completion before anything else happens. You can witness that by putting infinite loops in there. Only when there is "nothing else to do" will callbacks (or other things like onclick handlers) be executed. And they will also execute one after the other. There is only a single thread of execution (which on the plus side makes the programming model easier). Recommended reading: Introduction to node.js and its motivation.
1

x will be initialized first (Unless somehow the HTTP response comes back before it's able to execute the next line, which is extremely unlikely). $.ajax is asynchronous and it will take time for the response to come back.

If you wanted to guarantee that y was initialized first, you'd do this instead:

function method1() {
    method2(function()
    {
        var x = 1;
    });
}

function method2(callback) {
    $.ajax({
        type: "GET",
        async: true,
        url: "/MyController/MyMethod",
        success: function (data) {
            var y = 6;
            callback();
        }
    });
}

1 Comment

"Unless somehow the HTTP response comes back before it's able to execute the next line, which is extremely unlikely" I don't think this can ever happen, no matter how fast the response is. Javascript is single-threaded, and events/callbacks will be dealt with after the current code path is done (you will not get interrupted between lines).

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.