Skip to content

Commit

Permalink
doc: fix fs.promises sample codes
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
kakts authored and MylesBorins committed May 22, 2018
1 parent 37b9fe1 commit 8317a46
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -3443,6 +3443,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 @@ -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

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

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

0 comments on commit 8317a46

Please sign in to comment.