0

I wrote my first $.when routine but is not working as expected:

     $(document).ready(function () 
     {
      var   UserUrl = 'API/StartGetUser.asp';
      var   StoreUrl = 'API/StartGetStore.asp';
      var   FeedbackUrl='API/StartGetFeed.asp';
      var   ItemsUrl='API/StartGetSellers.asp';
    $.when(
        $.get(UserDetailsUrl),
        $.get(StoreCategoryUrl),
        $.get(FeedbackUrl),
        $.get(ItemsUrl)
    ).then(function(user,store,feed,items) {
        //$.get(mailUrl)
        alert(user+' '+store+' '+feed+' '+items);
    }).fail(function(err) {
      alert(err);
    });

It works, but I was expecting that the 4 get would have fired simultaneously, while I found that are executed one after the previous has finished... since is

$.get

it is surely async..

what can be wrong? What shall I check?

btw, serverside is classic asp and request are on same machine as can see from url.

thanks for any hints

Joe

Joe
  • 1,033
  • 1
  • 16
  • 39
  • I think you mean to say it is synchronous not async, but that doesn't really make sense. How do you know they aren't being sent immediately? – charlietfl Oct 21 '15 at 04:46
  • `$.get()` is not synchronous until you have done an `ajaxSetup({async: false})` – Arun P Johny Oct 21 '15 at 04:48
  • yes I mean it seems is working synchronously, but $.get works only async.. therefore it is not a problem of settings.. I see are not processed simultaneously since the effects are delayed – Joe Oct 21 '15 at 11:31

1 Answers1

4

The four requests are initiated one after the other because Javascript executes code sequentially. There is no actual parallel execution in Javascript (it is single threaded - except for webWorkers which are not involved here).

What will happen here is that all four requests will be "in process" at the same time. When exactly the responses come back is determined by the responsiveness of the server. Some servers will only handle one request at a time and will cause the responses to come back sequential, even though they were all initiated at once. Other servers will process them in a time-sliced fashion (or even using multiple processors) such that all responses are finished at about the same time.

You can verify the timing of the sending of the requests and the receiving of the responses by opening the network tab in the Chrome debugger before your operation and then looking at the time trace that it shows.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Hi, Thanks for your answer, but I'm little confused.... I found several article explaining that this was the way to get request to be executed simultaneously: [link](https://css-tricks.com/multiple-simultaneous-ajax-requests-one-callback-jquery/) [link](http://www.codeproject.com/Articles/599349/jQuery-Execute-Run-Multiple-Ajax-Requests-Simultan) [link](http://stackoverflow.com/questions/561046/how-many-concurrent-ajax-xmlhttprequest-requests-are-allowed-in-popular-browse) [jsfiddle](http://jsfiddle.net/jquerybyexample/tS349/) and my server can currently handle more request simultaneously – Joe Oct 21 '15 at 11:33
  • Anyway.. which can be the solution to enforce this? now when I need to execute some process quicker I fire the same page from 2 different browser, therefore I assume that Browser limits are based on sessionID: is it possible with jqyery ajax to handle sessionID? or is there some other technique to – Joe Oct 21 '15 at 11:39
  • @Joe - This is the general technique to get multiple Ajax requests running in parallel (if your server is capable of doing that). If you can post the network tab timeline from the Chrome debugger when running your code, we may be able to glean some info from that about what is going on. It is ultimately your server that determines how much in parallel they actually run so it will probably take instrumentation and debugging on your server to fully understand things. – jfriend00 Oct 21 '15 at 15:39
  • @Joe - browsers do have a limit to the maximum number of concurrent connections to the same host that they will open. That exact limit varies by browser, but for a modern browser that limit should certainly be above 3 (more likely in the range of 6-10) so if you have a bunch of other things going on to the same host (like image downloads), then you could be running into this limitation too. – jfriend00 Oct 21 '15 at 15:45
  • here Chrome Screenshot: ![IMG](http://tinypic.com/r/2ccrpqw/8) concerning server it is Windows Server 2012 R2 but running classic asp that do not allow more than 1 process with the same sessionID. (or perhaps it's me not capable of setting correctly) but if I access the same page with 4 different browser.. it process simultaneously all 4 requests. I thought jquery ajax would have its own Session ID.. but seems not.. Is there a way to change the sessionID with jquery ajax Request? Thanks for your time Joe – Joe Oct 21 '15 at 17:11
  • @Joe - That looks like the requests are sent to the server and then the client just waits for your server to respond. TTFB stands for time to first byte which is how long the client waits until the first byte of a response comes back. This looks like a server issue to me. – jfriend00 Oct 21 '15 at 17:35
  • yes process.asp is answered only once the first 2 calls (that download data from another website and can take even some hours) have finished. That's why I plan to build this routine to check progress and display a progress bar.. but If I get the details only when everything's finished.. it's useless.. What do you suggest to check? thanks again. – Joe Oct 21 '15 at 18:19
  • @Joe - If you control the server-side in addition to the client-side, then I'd suggest you open a webSocket connection and let the server send you progress messages directly rather than having you poll for progress. – jfriend00 Oct 21 '15 at 18:22
  • hi jfriend00. thanks for your suggestion.. but have no idea on how open a webSocket connection and how to handle :-( Have some indication where to check for some step by step instruction.. meanwhile I google a little..?
    beside this.. I solved: Problem is Session.. if session is enabled the server process the request syncrounsly.. just add `<%@ EnableSessionState=False %>` on top of each page and everything works like expected :-) Thanks again for your patience!
    – Joe Oct 21 '15 at 19:02