4

I am trying to understand the js event loop. This is my js code:

var start = new Date().getTime();
var url = "/WebForm1.aspx/Test1";
$.ajax({
    type: "POST",
    url: url,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        console.log('Test1, elapsed: ' + (new Date().getTime() - start) + 'ms');
    },
});

url = "/WebForm1.aspx/Test2";
$.ajax({
    type: "POST",
    url: url,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        console.log('Test2, elapsed: ' + (new Date().getTime() - start) + 'ms');
    },
});

url = "/WebForm1.aspx/Test3";
$.ajax({
    type: "POST",
    url: url,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        console.log('Test3, elapsed: ' + (new Date().getTime() - start) + 'ms');
    },
});

This is my server side code:

[WebMethod]
public static void Test1()
{
    System.Threading.Thread.Sleep(1500);
}

[WebMethod]
public static void Test2()
{
    System.Threading.Thread.Sleep(2000);
}

[WebMethod]
public static void Test3()
{
    System.Threading.Thread.Sleep(3000);
}

Now the result is :

Test1, elapsed: 1542ms
Test3, elapsed: 4578ms
Test2, elapsed: 6636ms

Now what i dont understand is, why doesnt they all execute together? is it because the server side can process only one request at a time, or does it revolves with the js event loop?

9
  • looks like the server side is single threaded to me Commented Sep 8, 2015 at 14:05
  • 1
    what do you mean "looks like" :) is it single threaded or not Commented Sep 8, 2015 at 14:06
  • The ajax calls should fire off rapidly in sequence, but if the server is blocking and handling them serially, each has to wait its turn. It's server-side. Commented Sep 8, 2015 at 14:06
  • 5
    stackoverflow.com/questions/4428413/… Commented Sep 8, 2015 at 14:07
  • So until the server side finishes one request, all other requests are stuck? Commented Sep 8, 2015 at 14:08

2 Answers 2

2

What this experiment shows is that the server handles the requests one-by-one, instead of in parallel. Why this is is not immediately apparent.

It's possible that the server is single threaded. Another possibility is that the requests require a shared (locked) resource for instance the user session or a locked database table.

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

2 Comments

as i see from @Stryner link its because it uses the same session id
Possibly, I see nothing in your code that indicates sessions are used though. Is there some wrapper code that does?
0

The two arguments are correct. Your server can handle just one request at a time (with the same origin), and javascript has to wait for the server. Javascript anyways will execute one response at time, because it's single threaded.

3 Comments

Your server can handle just one request at a time - proof? Javascript anyways will execute one request at time - no (unless asynch=false is passed, which it is not)
Because is how asp.net works stackoverflow.com/questions/1735107/… , and you are right with javascript, it could execute n requests, but responses will be handled one by one. It's more about server side.
This has nothing to do with asp.net, but with how sessions work; or locked resource in general. You see the same behaviour in PHP. If you close the session before you sleep(), the other requests wont need to wait.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.