diff --git a/lib/js/src/thrift.js b/lib/js/src/thrift.js index 09cccc3d3c2..441ba44babd 100644 --- a/lib/js/src/thrift.js +++ b/lib/js/src/thrift.js @@ -361,32 +361,26 @@ Thrift.TXHRTransport.prototype = { } if (callback) { + var clientCallback = callback; + var onError = function() { + // Clear the previous response before invoking the callback. + self.setRecvBuffer(''); + clientCallback(); + }; + // Wait for the request to finish before invoking the callback. - xreq.onreadystatechange = - (function() { - var clientCallback = callback; - return function() { - if (this.readyState === 4 && this.status === 200) { - self.setRecvBuffer(this.responseText); + xreq.onreadystatechange = function() { + if (xreq.readyState === 4 && xreq.status === 200) { + self.setRecvBuffer(xreq.responseText); clientCallback(); - } else if (this.readyState === 4 && this.status !== 0) { - this.onerror(); - } - }; - }()); - - xreq.onerror = - (function() { - var clientCallback = callback; - return function() { - // Clear the previous response before invoking the callback. - self.setRecvBuffer(''); - clientCallback(); - }; - }()); - - xreq.ontimeout = xreq.onerror; - xreq.onabort = xreq.onerror; + } else if (xreq.readyState === 4 && xreq.status !== 0) { + onError(); + } + }; + + xreq.onerror = onError; + xreq.ontimeout = onError; + xreq.onabort = onError; } xreq.open('POST', this.url, !!async);