Skip to content

Commit f68fed2

Browse files
ahoymrvagg
authored andcommitted
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>
1 parent cb971cc commit f68fed2

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

lib/_http_client.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -504,21 +504,23 @@ ClientRequest.prototype._deferToConnect = function(method, arguments_, cb) {
504504
// in the future (when a socket gets assigned out of the pool and is
505505
// eventually writable).
506506
var self = this;
507+
508+
function callSocketMethod() {
509+
if (method)
510+
self.socket[method].apply(self.socket, arguments_);
511+
512+
if (typeof cb === 'function')
513+
cb();
514+
}
515+
507516
var onSocket = function() {
508517
if (self.socket.writable) {
509-
if (method) {
510-
self.socket[method].apply(self.socket, arguments_);
511-
}
512-
if (cb) { cb(); }
518+
callSocketMethod();
513519
} else {
514-
self.socket.once('connect', function() {
515-
if (method) {
516-
self.socket[method].apply(self.socket, arguments_);
517-
}
518-
if (cb) { cb(); }
519-
});
520+
self.socket.once('connect', callSocketMethod);
520521
}
521522
};
523+
522524
if (!self.socket) {
523525
self.once('socket', onSocket);
524526
} else {

0 commit comments

Comments
 (0)