Skip to content

Commit

Permalink
http: remove redundant code in _deferToConnect
Browse files Browse the repository at this point in the history
Logic for calling the passed in socket method and/or callback
was duplicated. This commit refactors the relevant code to
remove the redundancy.

PR-URL: #2769
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Brendan Ashworth <brendan.ashworth@me.com>
  • Loading branch information
ahoym authored and rvagg committed Sep 22, 2015
1 parent cb971cc commit f68fed2
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lib/_http_client.js
Expand Up @@ -504,21 +504,23 @@ 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;

function callSocketMethod() {
if (method)
self.socket[method].apply(self.socket, arguments_);

if (typeof cb === 'function')
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) {
self.once('socket', onSocket);
} else {
Expand Down

0 comments on commit f68fed2

Please sign in to comment.