2

I am using asp.net-generated javascript wrappers for asp.net web services. In javascript, how to "wait" for several async web services wrappers to finished returning some json data? Is there a general and elegant way in javascript to deal with waiting for async functions ?

I have tried using jquery $.ajax , which has an option to use sync rather than async. What is the pro and cons for using sync $.ajax mode ?

I know that each wrapper takes a success event handler and a failure event handler . But I found the nesting of many success event handlers hard to read . I wonder if other users find the nesting awkward ...

sikender
  • 5,883
  • 7
  • 42
  • 80
user670800
  • 1,107
  • 1
  • 11
  • 16
  • I have put a few ideas out for review at [http://stackoverflow.com/questions/8211687/structure-async-callbacks-in-javascript-synching-the-asynch][1] [1]: http://stackoverflow.com/questions/8211687/structure-async-callbacks-in-javascript-synching-the-asynch – bnieland Nov 21 '11 at 12:24

1 Answers1

2

What is the pro and cons for using sync $.ajax mode ?

The con is that in all browsers (except Chrome, AFAIK), there's only a single thread for processing JavaScript, so while you wait synchronously for your web service call to complete, your page will be completely unresponsive. This is the type of situation that leads to an "unresponsive script" warning in the browser that can end up causing users to halt your JavaScript while it's executing, or close your site completely.

Joel C
  • 5,547
  • 1
  • 21
  • 31
  • what is the proper way to implement synchronization if the calls are async with callback ? I suppose even async calls can take a long to return ; in case of asp.net webservice js wrappers, it there a time out options/callback ? – user670800 Apr 13 '11 at 01:23
  • In jquery you can set an error callback either globally or on each request, and one of the arguments will be the status (either "timeout", "error", "abort", or "parsererror"). You can also set the timeout either globally or on each request. See the documentation: http://api.jquery.com/jQuery.ajax/ – Joel C Apr 13 '11 at 04:00