diff --git a/lib/_http_client.js b/lib/_http_client.js index e490dce5d821d6..dffc6b77a27500 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; + 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) {