diff --git a/lib/net.js b/lib/net.js index 78aa756a358a71..9681616df62799 100644 --- a/lib/net.js +++ b/lib/net.js @@ -517,17 +517,17 @@ Socket.prototype._onTimeout = function() { Socket.prototype.setNoDelay = function(enable) { + // Backwards compatibility: assume true when `enable` is omitted + enable = Boolean(enable === undefined ? true : enable); + if (!this._handle) { - this.once('connect', - enable ? this.setNoDelay : () => this.setNoDelay(enable)); + this[kSetNoDelay] = enable; return this; } - // Backwards compatibility: assume true when `enable` is omitted - const newValue = enable === undefined ? true : !!enable; - if (this._handle.setNoDelay && newValue !== this[kSetNoDelay]) { - this[kSetNoDelay] = newValue; - this._handle.setNoDelay(newValue); + if (this._handle.setNoDelay && enable !== this[kSetNoDelay]) { + this[kSetNoDelay] = enable; + this._handle.setNoDelay(enable); } return this; @@ -535,13 +535,16 @@ Socket.prototype.setNoDelay = function(enable) { Socket.prototype.setKeepAlive = function(enable, initialDelayMsecs) { + enable = Boolean(enable); + const initialDelay = ~~(initialDelayMsecs / 1000); + if (!this._handle) { - this.once('connect', () => this.setKeepAlive(enable, initialDelayMsecs)); + this[kSetKeepAlive] = enable; + this[kSetKeepAliveInitialDelay] = initialDelay; return this; } if (this._handle.setKeepAlive && enable !== this[kSetKeepAlive]) { - const initialDelay = ~~(initialDelayMsecs / 1000); this[kSetKeepAlive] = enable; this[kSetKeepAliveInitialDelay] = initialDelay; this._handle.setKeepAlive(enable, initialDelay);