From ed248867545505598c0e8a5c3e96cbe67159146a Mon Sep 17 00:00:00 2001 From: Keita Akutsu Date: Sun, 20 May 2018 02:45:26 +0900 Subject: [PATCH 1/2] doc: fix fs.promises sample codes. --- doc/api/fs.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 55691dedac19b0..6691b7b9138472 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -3453,6 +3453,7 @@ added: v10.0.0 Closes the file descriptor. ```js +const fsPromises = require('fs').promises; async function openAndClose() { let filehandle; try { @@ -3564,6 +3565,9 @@ For example, the following program retains only the first four bytes of the file: ```js +const fs = require('fs'); +const fsPromises = fs.promises; + console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints: Node.js @@ -3580,6 +3584,9 @@ If the file previously was shorter than `len` bytes, it is extended, and the extended part is filled with null bytes (`'\0'`). For example, ```js +const fs = require('fs'); +const fsPromises = fs.promises; + console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints: Node.js @@ -3684,6 +3691,9 @@ with an `Error` object. The following example checks if the file `/etc/passwd` can be read and written by the current process. ```js +const fs = require('fs'); +const fsPromises = fs.promises; + fsPromises.access('/etc/passwd', fs.constants.R_OK | fs.constants.W_OK) .then(() => console.log('can access')) .catch(() => console.error('cannot access')); @@ -3776,7 +3786,7 @@ then the operation will fail. Example: ```js -const fs = require('fs'); +const fsPromises = require('fs').promises; // destination.txt will be created or overwritten by default. fsPromises.copyFile('source.txt', 'destination.txt') @@ -3788,7 +3798,7 @@ If the third argument is a number, then it specifies `flags`, as shown in the following example. ```js -const fs = require('fs'); +const fsPromises = require('fs').promises; const { COPYFILE_EXCL } = fs.constants; // By using COPYFILE_EXCL, the operation will fail if destination.txt exists. From 726be355b732e52545cd79da21eef0fe3f4d61fb Mon Sep 17 00:00:00 2001 From: Keita Akutsu Date: Sun, 20 May 2018 13:07:00 +0900 Subject: [PATCH 2/2] doc: fix fs.promises.copyFile sample code. --- doc/api/fs.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 6691b7b9138472..54f72f425e278e 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -3798,7 +3798,8 @@ If the third argument is a number, then it specifies `flags`, as shown in the following example. ```js -const fsPromises = require('fs').promises; +const fs = require('fs'); +const fsPromises = fs.promises; const { COPYFILE_EXCL } = fs.constants; // By using COPYFILE_EXCL, the operation will fail if destination.txt exists.