Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: fix fs.promises sample codes. #20838

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -3453,6 +3453,7 @@ added: v10.0.0
Closes the file descriptor.

```js
const fsPromises = require('fs').promises;
async function openAndClose() {
let filehandle;
try {
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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'));
Expand Down Expand Up @@ -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')
Expand All @@ -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.
Expand Down