Skip to content

Commit 7295d3b

Browse files
kaktsvsemozhetbyt
authored andcommitted
doc: fix fs.promises sample codes
PR-URL: #20838 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent e270ae9 commit 7295d3b

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

doc/api/fs.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3453,6 +3453,7 @@ added: v10.0.0
34533453
Closes the file descriptor.
34543454

34553455
```js
3456+
const fsPromises = require('fs').promises;
34563457
async function openAndClose() {
34573458
let filehandle;
34583459
try {
@@ -3564,6 +3565,9 @@ For example, the following program retains only the first four bytes of the
35643565
file:
35653566

35663567
```js
3568+
const fs = require('fs');
3569+
const fsPromises = fs.promises;
3570+
35673571
console.log(fs.readFileSync('temp.txt', 'utf8'));
35683572
// Prints: Node.js
35693573

@@ -3580,6 +3584,9 @@ If the file previously was shorter than `len` bytes, it is extended, and the
35803584
extended part is filled with null bytes (`'\0'`). For example,
35813585

35823586
```js
3587+
const fs = require('fs');
3588+
const fsPromises = fs.promises;
3589+
35833590
console.log(fs.readFileSync('temp.txt', 'utf8'));
35843591
// Prints: Node.js
35853592

@@ -3684,6 +3691,9 @@ with an `Error` object. The following example checks if the file
36843691
`/etc/passwd` can be read and written by the current process.
36853692

36863693
```js
3694+
const fs = require('fs');
3695+
const fsPromises = fs.promises;
3696+
36873697
fsPromises.access('/etc/passwd', fs.constants.R_OK | fs.constants.W_OK)
36883698
.then(() => console.log('can access'))
36893699
.catch(() => console.error('cannot access'));
@@ -3776,7 +3786,7 @@ then the operation will fail.
37763786
Example:
37773787

37783788
```js
3779-
const fs = require('fs');
3789+
const fsPromises = require('fs').promises;
37803790

37813791
// destination.txt will be created or overwritten by default.
37823792
fsPromises.copyFile('source.txt', 'destination.txt')
@@ -3789,6 +3799,7 @@ following example.
37893799

37903800
```js
37913801
const fs = require('fs');
3802+
const fsPromises = fs.promises;
37923803
const { COPYFILE_EXCL } = fs.constants;
37933804

37943805
// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.

0 commit comments

Comments
 (0)