Skip to content
Permalink
Browse files

http: remove redundant code in _deferToConnect

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 9, 2015
1 parent cb971cc commit f68fed2e6ff008cd0464831d4b1f081be1f65efc
Showing with 12 additions and 10 deletions.
  1. +12 −10 lib/_http_client.js
@@ -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 {

0 comments on commit f68fed2

Please sign in to comment.
You can’t perform that action at this time.