THRIFT-2350 Add async calls to normal JavaScript
Patch: Randy Abernethy
diff --git a/lib/js/src/thrift.js b/lib/js/src/thrift.js
index 4e1f20f..d605ab7 100644
--- a/lib/js/src/thrift.js
+++ b/lib/js/src/thrift.js
@@ -306,12 +306,13 @@
      * or the async parameter is True or the URL is an empty string, the current 
      * send buffer is returned.
      * @param {object} async - If true the current send buffer is returned.
+     * @param {object} callback - Optional async callback function, receives the call result.
      * @returns {undefined|string} Nothing or the current send buffer.
      * @throws {string} If XHR fails.
      */
-    flush: function(async) {
+    flush: function(async, callback) {
         //async mode
-        if (async || this.url === undefined || this.url === '') {
+        if ((async && !callback) || this.url === undefined || this.url === '') {
             return this.send_buf;
         }
 
@@ -321,8 +322,15 @@
             xreq.overrideMimeType('application/json');
         }
 
-        xreq.open('POST', this.url, false);
+        if (callback) {
+            xreq.onreadystatechange = callback;
+        }
+
+        xreq.open('POST', this.url, !!async);
         xreq.send(this.send_buf);
+        if (async && callback) {
+            return;
+        }
 
         if (xreq.readyState != 4) {
             throw 'encountered an unknown ajax ready state: ' + xreq.readyState;
@@ -1152,3 +1160,4 @@
     return client;
 };
 
+