mirror of
https://github.com/nodejs/node.git
synced 2026-11-21 12:59:16 -05:00
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:
1 parent
e270ae9f01
commit
7295d3ba6b
1 file changed
+12
-1
+12
-1
@@ -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')
|
||||
@@ -3789,6 +3799,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.
|
||||
|
||||
Reference in new issue
Block a user