diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index 18a610feab9c4c..f28aecae710cef 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -546,14 +546,6 @@ OutgoingMessage.prototype.end = function(data, encoding, callback) { return false; } - var self = this; - function finish() { - self.emit('finish'); - } - - if (typeof callback === 'function') - this.once('finish', callback); - if (!this._header) { if (data) { if (typeof data === 'string') @@ -581,6 +573,13 @@ OutgoingMessage.prototype.end = function(data, encoding, callback) { this.write(data, encoding); } + if (typeof callback === 'function') + this.once('finish', callback); + + const finish = () => { + this.emit('finish'); + }; + if (this._hasBody && this.chunkedEncoding) { ret = this._send('0\r\n' + this._trailer + '\r\n', 'latin1', finish); } else {