From 3c09d1b55f610573d25c21a157546d18b6fcd299 Mon Sep 17 00:00:00 2001 From: Guy Fraser Date: Thu, 23 Jun 2016 00:35:57 +0100 Subject: [PATCH] http: replace finish() callback with arrow function Take advantage of arrow function lexical `this` to avoid defining a `self = this` var which was only used once. Code relating to the `finish` event was split in to two areas of the parent function. Gathered it together to clarify association within the script. Fixes: https://github.com/nodejs/node/issues/7295 PR-URL: https://github.com/nodejs/node/pull/7378 Reviewed-By: Stephen Belanger Reviewed-By: Brian White Reviewed-By: Fedor Indutny Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- lib/_http_outgoing.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index 18a610feab9..f28aecae710 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 {