doc: fix fs.promises sample codes

PR-URL: https://github.com/nodejs/node/pull/20838
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
Keita Akutsu authored and Myles Borins committed 2018-05-22 14:28:05 -04:00
1 parent 37b9fe1e68
commit 8317a468db
1 file changed
+12 -1
+12 -1
View File
@@ -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.