Skip to content

Commit

Permalink
doc: use shorter fs.copyFile examples
Browse files Browse the repository at this point in the history
This consolidates some examples to concentrate the reader on the
important aspects and to reduce reading overhead.

PR-URL: nodejs#27044
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
BridgeAR committed Jan 12, 2020
1 parent a13500f commit 4455f60
Showing 1 changed file with 12 additions and 25 deletions.
37 changes: 12 additions & 25 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1588,19 +1588,15 @@ then the operation will fail.

```js
const fs = require('fs');
const { COPYFILE_EXCL } = fs.constants;

// destination.txt will be created or overwritten by default.
fs.copyFile('source.txt', 'destination.txt', (err) => {
function callback(err) {
if (err) throw err;
console.log('source.txt was copied to destination.txt');
});
```

If the third argument is a number, then it specifies `mode`:
}

```js
const fs = require('fs');
const { COPYFILE_EXCL } = fs.constants;
// destination.txt will be created or overwritten by default.
fs.copyFile('source.txt', 'destination.txt', callback);

// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.
fs.copyFile('source.txt', 'destination.txt', COPYFILE_EXCL, callback);
Expand Down Expand Up @@ -1636,17 +1632,11 @@ then the operation will fail.

```js
const fs = require('fs');
const { COPYFILE_EXCL } = fs.constants;

// destination.txt will be created or overwritten by default.
fs.copyFileSync('source.txt', 'destination.txt');
console.log('source.txt was copied to destination.txt');
```

If the third argument is a number, then it specifies `mode`:

```js
const fs = require('fs');
const { COPYFILE_EXCL } = fs.constants;

// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.
fs.copyFileSync('source.txt', 'destination.txt', COPYFILE_EXCL);
Expand Down Expand Up @@ -4718,20 +4708,17 @@ create a copy-on-write reflink. If the platform does not support copy-on-write,
then the operation will fail.

```js
const fsPromises = require('fs').promises;
const {
promises: fsPromises,
constants: {
COPYFILE_EXCL
}
} = require('fs');

// destination.txt will be created or overwritten by default.
fsPromises.copyFile('source.txt', 'destination.txt')
.then(() => console.log('source.txt was copied to destination.txt'))
.catch(() => console.log('The file could not be copied'));
```

If the third argument is a number, then it specifies `mode`:

```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.
fsPromises.copyFile('source.txt', 'destination.txt', COPYFILE_EXCL)
Expand Down

0 comments on commit 4455f60

Please sign in to comment.