From 8317a468db5bf118b06e8dbcc8875abc284185fb Mon Sep 17 00:00:00 2001 From: Keita Akutsu Date: Sun, 20 May 2018 02:45:26 +0900 Subject: [PATCH] doc: fix fs.promises sample codes PR-URL: /~https://github.com/nodejs/node/pull/20838 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Vse Mozhet Byt Reviewed-By: Trivikram Kamat --- doc/api/fs.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 286ffb3a63fb3a..cfa21733069698 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -3443,6 +3443,7 @@ added: v10.0.0 Closes the file descriptor. ```js +const fsPromises = require('fs').promises; async function openAndClose() { let filehandle; try { @@ -3554,6 +3555,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 @@ -3570,6 +3574,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 @@ -3674,6 +3681,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')); @@ -3766,7 +3776,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') @@ -3779,6 +3789,7 @@ following example. ```js 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.