views:

223

answers:

9

It seems most everyone does asynchronous requests with XMLHttpRequest but obviously the fact that there is the ability to do synchronous requests indicates there might be a valid reason to do so. So what might that valid reason be?

+2  A: 

I'd say that if you consider blocking the user's browser while the request completes acceptable, then sure use a synchronous request.

If serialization of requests is your aim, then this can be accomplished using async requests, by having the onComplete callback of your previous request fire the next in line.

hobodave
Hmm, I'd add a sliding window to that, otherwise you'll basically lose the round-trip time for each request
Stephan Eggermont
+1  A: 

What happens if you make a synchronous call in production code?

The sky falls down.

No seriously, the user does not like a locked up browser.

martinr
The question is "why do it with XMLHTTPREQUEST" not "why do or not do sync calls"
Itay Moav
+5  A: 

I think they might become more popular as HTML 5 standards progress. If a web application is given access to multiple threads, I could foresee developers using a dedicated thread to make synchronous requests for, as Jonathan said, to ensure one request happens before another. With the current situation of one thread, it is a less than ideal design as it blocks until the request is complete.

darren
What part of HTML5 adds threads?
Weston C
I assume he meant 'Web Workers'
evilpie
+1  A: 

I use it to validate a username, during the check that the username does not exist already.

I know it would be better to do that asynchronously, but then I should use a different code for this particular validation rule. I explain better. My validation setup uses some validation functions, which return true or false, depending if the data is valid.

Since the function has to return, I cannot use asynchronous techniques, so I just make that synchronous and hope that the server will answer promptly enough not to be too noticeable. If I used an AJAX callback, then I would have to handle the rest of the execution differently from the other validation methods.

Andrea
+2  A: 

Sometimes you have an action that depends in others. For example, action B can only be started if A is finished. The synchronous approach is usually used to avoid race conditions. Sometimes using a synchronous call is a simpler implementation then creating complex logic to check every state of your asynchronous calls that depend on each other.

The problem with this approach is that you "block" the user's browser until the action is finished (until the request returns, finishes, loads, etc). So be careful when using it.

GmonC
The question is "why do it with XMLHTTPREQUEST" not "why do sync calls"
Itay Moav
The author himself says "**Synchronous** calls block the browser which leads to a terrible user experience." in a comment, so everyone in this thread applied for the "synchronous calls" approach. Why did you take this literal understanding of the question if you can't really say for sure what the author meant?
GmonC
+1  A: 

I use synchronous calls when developing code- whatever you did while the request was commuting to and from the server can obscure the cause of an error.

When it's working, I make it asynchronous, but I try to include an abort timer and failure callbacks, cause you never know...

kennebec
+1  A: 

XMLHTTPREQUEST is usually for async. Some times (for debugging, or specific business logic) you would like to change all/several of the async calls in one page to sync.
You would like to do it without changing everything in your JS code. The async/sync flag gives you that ability, and if designed correctly, you need only change one line in your code/change the value of one var during run time.

Itay Moav
+5  A: 

You'd want to perform synchronous calls in any sort of transaction-like processing, or wherever any order of operation is necessary.

For instance, let's say you want to customize an event to log you out after playing a song. If the logout operation occurs first, then the song will never be played. This requires synchronizing the requests.

Another reason would be when working with a WebService, especially when performing math on the server.

Example: Server has a variable with value of 1.

 Step (1) Perform Update: add 1 to variable
 Step (2) Perform Update: set variable to the power of 3
 End Value: variable equals 8

If Step (2) occurs first, then the end value is 2, not 8; thus order of operation matters and synchronization is needed.


There are very few times that a synchronous call may be justified in a common real world example. Perhaps when clicking login and then clicking a portion of the site that requires a user to be logged in.

As others have said, it will tie up your browser, so stay away from it where you can.

Instead of synchronous calls, though, often users want to stop an event that is currently loading and then perform some other operation. In a way this is synchronization, since the first event is quit before the second begins. To do this, use the abort() method on the xml connection object.

vol7ron
I think this should be the true answer
Armando
We use synchronous calls on our intranet's "contacts" and "favorites" pages, where we need to ensure a user's data is loaded before we can update the DOM.
Zack Mulgrew
@Zack: that's a good example, however you could probably do the same with asynchronous calls depending on the event that is calling it.
vol7ron
+1  A: 

I can see a use for synchronous XHR requests to be used when a resource in a variable location must be loaded before other static resources in the page that depend on the first resource to fully function. In point of fact, I'm implementing such an XHR request in a little sub-project of my own whereas JavaScript resources reside in variable locations on the server depending on a set of specific parameters. Subsequent JavaScript resources rely on those variable resources and such files MUST be guaranteed to load before the other reliant files are loaded, thus making the application whole.

That idea foundation really kind of expands on vol7ron's answer. Transaction-based procedures are really the only time where synchronous requests should be made. In most other cases, asynchronous calls are the better alternative in which, after the call, the DOM is updated as necessary. In many cases, such as user-based systems, you could have certain features locked to "unauthorized users" until they have, per se, logged in. The those features, after the asynchronous call, are unlocked via a DOM update procedure.

I'd have to finally say that I agree with most individuals' points on the matter: wherever possible, synchronous XHR requests should be avoided as, with the way it works, the browser locks up with synchronous calls. When implementing synchronous requests, they should be done in a manner where the browser would normally be locked, anyway, say in the HEAD section before page loading actually occurs.

hyponiq