diff --git a/lib/fs/promises.js b/lib/fs/promises.js index a36d845d5177fa..8884777e5e16de 100644 --- a/lib/fs/promises.js +++ b/lib/fs/promises.js @@ -157,7 +157,12 @@ async function readFileHandle(filehandle, options) { chunks.push(buffer.slice(0, totalRead)); } while (totalRead === chunkSize); - return Buffer.concat(chunks); + const result = Buffer.concat(chunks); + if (options.encoding) { + return result.toString(options.encoding); + } else { + return result; + } } // All of the functions are defined as async in order to ensure that errors diff --git a/test/parallel/test-fs-promises-writefile.js b/test/parallel/test-fs-promises-writefile.js index e2ae289b180bc2..280bce864229ce 100644 --- a/test/parallel/test-fs-promises-writefile.js +++ b/test/parallel/test-fs-promises-writefile.js @@ -35,7 +35,15 @@ async function doRead() { assert.deepStrictEqual(buf, data); } +async function doReadWithEncoding() { + const data = await fsPromises.readFile(dest, 'utf-8'); + const syncData = fs.readFileSync(dest, 'utf-8'); + assert.strictEqual(typeof data, 'string'); + assert.deepStrictEqual(data, syncData); +} + doWrite() .then(doAppend) .then(doRead) + .then(doReadWithEncoding) .then(common.mustCall());