Permalink
Browse files

refactored code into _fireCancelTimeout, which is now also called whe…

…n an error is emitted.
  • Loading branch information...
1 parent e2b0fc6 commit 2e031252d51c030dcc4a2ed8d026d6b1e8197ff7 @bosky101 committed Sep 4, 2013
Showing with 11 additions and 5 deletions.
  1. +11 −5 lib/restler.js
View
@@ -206,9 +206,16 @@ mixin(Request.prototype, {
}
},
_fireError: function(err, response) {
+ this._fireCancelTimeout();
this.emit('error', err, response);
this.emit('complete', err, response);
},
+ _fireCancelTimeout: function(){
+ var self = this;
+ if(self.options.timeout){
+ clearTimeout(self.options.timeoutFn);
+ }
+ },
_fireTimeout: function(err){
this.emit('timeout', err);
this.aborted = true;
@@ -227,19 +234,18 @@ mixin(Request.prototype, {
},
_makeRequest: function() {
var self = this;
- var timeoutMs = self.options.timeout,timeout;
+ var timeoutMs = self.options.timeout;
if(timeoutMs){
- self.options.timeout = setTimeout(function(){
+ self.options.timeoutFn = setTimeout(function(){
self._fireTimeout(timeoutMs);
},timeoutMs);
}
this.request.on('response', function(response) {
- if(self.options.timeout){
- clearTimeout(self.options.timeout);
- }
+ self._fireCancelTimeout();
self.emit('response', response);
self._responseHandler(response);
}).on('error', function(err) {
+ self._fireCancelTimeout();
if (!self.aborted) {
self._fireError(err, null);
}

0 comments on commit 2e03125

Please sign in to comment.