Google Closure’s idiocies: Ajax can’t be synchronous :(

While I love Closure tools and the possibility to write a better organized and OOP based JavaScript, I’m facing some weakness and inexplicable choices in the library. The last discover is that is impossible to make synchronous ajax call using the provided classes, since I didn’t find nothing about synch/asynch options by reading API reference, I took a look to the source (xhrio.js), and I noticed this:, url, true);  // Always async!

that true should be a parametric value which developers can set to true or false. Why forcing it to true???
I’m really disappointed :(

  • gsziszi

    ajax never can be synchronous:
    Asynchronous JavaScript and XML

  • LOL :^) XmlHttpRequest can be opened both in synchronous (asynchronous flag set to “false”) than asynchronous mode (asynchronous flag set to “true”)

  • gsziszi

    That is correct, but XmlHttpRequest is a method, AJAX is a technology based on that method.
    Otherwise, the JSON version of AJAX is AJAJ, however we use also XmlHttpRequest not JsonHttpRequest. So, we should be cautious when we use these words and abbreviations.

  • XmlHttpRequest is not a method, is an object and it has methods like open(). By saying “ajax” I merely mean an xhr call to retrieve data on demand, this data can be plain text, json, xml or something else and can be retrieved synchronously or asynchronously, but from a generic point of view is always ajax (the term is very generic)… I hope I was clear :^)

  • gsziszi

    I accept

  • You can create a XmlHttpRequest with and pass true in the third argument of the open method.

  • Linus Ericsson

    The reason for this implementation quirk seems to be that Google Closure team (and all Google) really want that rich websites using Google’s tools should be blazing fast. Synchronous XHRs is (given javascripts single execution thread) bound to lock the whole execution until one get a reply and would give very sloppy performance for the gui.

  • It’s always a plueasre to hear from someone with expertise.

  • Yevhen Pavliuk

    Here’s a small workaround:



    * An XML HTTP factory that creates synchronous XHRs regardless of which option
    * (sync/async) is actually used. The factory overcomes the Closure Library
    * limitation that only asynchronous XHRs are created.
    * var syncXhr = new;
    * @constructor
    * @extends {}
    */ = function() {

    * Always opens a synchronous XHR.
    * @this {!XMLHttpRequest}
    * @param {string} method
    * @param {string} url
    * @private
    */ = function(method, url) {, method, url, false /* Is async? */);

    /** @override */ = function() {
    var instance;

    instance = goog.base(this, ‘createInstance’); =;

    return instance;