From 073ec80dedda21849a10b08b18784cdea0ac0250 Mon Sep 17 00:00:00 2001 From: Malcolm Ahoy Date: Wed, 9 Sep 2015 06:11:19 -0700 Subject: [PATCH 1/3] http: DRY ClientRequest.prototype._deferToConnect Logic for calling the passed in socket method and/or callback was duplicated. --- lib/_http_client.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/_http_client.js b/lib/_http_client.js index e490dce5d821d6..1809b78d3f81e7 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -504,19 +504,17 @@ ClientRequest.prototype._deferToConnect = function(method, arguments_, cb) { // in the future (when a socket gets assigned out of the pool and is // eventually writable). var self = this; + var callSocketMethod = function() { + if (method) { + self.socket[method].apply(self.socket, arguments_); + } + if (cb) { cb(); } + }; var onSocket = function() { if (self.socket.writable) { - if (method) { - self.socket[method].apply(self.socket, arguments_); - } - if (cb) { cb(); } + callSocketMethod(); } else { - self.socket.once('connect', function() { - if (method) { - self.socket[method].apply(self.socket, arguments_); - } - if (cb) { cb(); } - }); + self.socket.once('connect', callSocketMethod); } }; if (!self.socket) { From 66ddd8872f600c1985a5039289ee465318108431 Mon Sep 17 00:00:00 2001 From: Malcolm Ahoy Date: Wed, 9 Sep 2015 07:44:46 -0700 Subject: [PATCH 2/3] Prefer fn declaration and braceless if statements --- lib/_http_client.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/_http_client.js b/lib/_http_client.js index 1809b78d3f81e7..cea25fb230d112 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -504,11 +504,11 @@ ClientRequest.prototype._deferToConnect = function(method, arguments_, cb) { // in the future (when a socket gets assigned out of the pool and is // eventually writable). var self = this; - var callSocketMethod = function() { - if (method) { + function callSocketMethod() { + if (method) self.socket[method].apply(self.socket, arguments_); - } - if (cb) { cb(); } + if (typeof cb === 'function') + cb(); }; var onSocket = function() { if (self.socket.writable) { From 1f20e3057799c1061085655a66d0e034e7517302 Mon Sep 17 00:00:00 2001 From: Malcolm Ahoy Date: Wed, 9 Sep 2015 07:55:09 -0700 Subject: [PATCH 3/3] Remove unnecessary semi-colon --- lib/_http_client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/_http_client.js b/lib/_http_client.js index cea25fb230d112..dffc6b77a27500 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -509,7 +509,7 @@ ClientRequest.prototype._deferToConnect = function(method, arguments_, cb) { self.socket[method].apply(self.socket, arguments_); if (typeof cb === 'function') cb(); - }; + } var onSocket = function() { if (self.socket.writable) { callSocketMethod();