From a68b8b708f04e1bb6c4af9226424fd871a1e3ab6 Mon Sep 17 00:00:00 2001 From: Weijia Wang <381152119@qq.com> Date: Mon, 20 Nov 2017 00:11:24 +0800 Subject: [PATCH] fs: use arrow functions instead of `.bind` and `self` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/17137 Reviewed-By: Michaƫl Zasso Reviewed-By: Gireesh Punathil Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- lib/fs.js | 48 ++++++++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 01edc7411a5..a53290274c3 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -2091,30 +2091,27 @@ ReadStream.prototype._read = function(n) { return this.push(null); // the actual read. - var self = this; - fs.read(this.fd, pool, pool.used, toRead, this.pos, onread); + fs.read(this.fd, pool, pool.used, toRead, this.pos, (er, bytesRead) => { + if (er) { + if (this.autoClose) { + this.destroy(); + } + this.emit('error', er); + } else { + var b = null; + if (bytesRead > 0) { + this.bytesRead += bytesRead; + b = thisPool.slice(start, start + bytesRead); + } + + this.push(b); + } + }); // move the pool positions, and internal position for reading. if (this.pos !== undefined) this.pos += toRead; pool.used += toRead; - - function onread(er, bytesRead) { - if (er) { - if (self.autoClose) { - self.destroy(); - } - self.emit('error', er); - } else { - var b = null; - if (bytesRead > 0) { - self.bytesRead += bytesRead; - b = thisPool.slice(start, start + bytesRead); - } - - self.push(b); - } - } }; ReadStream.prototype._destroy = function(err, cb) { @@ -2209,7 +2206,7 @@ fs.FileWriteStream = fs.WriteStream; // support the legacy name WriteStream.prototype.open = function() { - fs.open(this.path, this.flags, this.mode, function(er, fd) { + fs.open(this.path, this.flags, this.mode, (er, fd) => { if (er) { if (this.autoClose) { this.destroy(); @@ -2220,7 +2217,7 @@ WriteStream.prototype.open = function() { this.fd = fd; this.emit('open', fd); - }.bind(this)); + }); }; @@ -2234,15 +2231,14 @@ WriteStream.prototype._write = function(data, encoding, cb) { }); } - var self = this; - fs.write(this.fd, data, 0, data.length, this.pos, function(er, bytes) { + fs.write(this.fd, data, 0, data.length, this.pos, (er, bytes) => { if (er) { - if (self.autoClose) { - self.destroy(); + if (this.autoClose) { + this.destroy(); } return cb(er); } - self.bytesWritten += bytes; + this.bytesWritten += bytes; cb(); });