Skip to content

Commit 4455f60

Browse files
committed
doc: use shorter fs.copyFile examples
This consolidates some examples to concentrate the reader on the important aspects and to reduce reading overhead. PR-URL: #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]>
1 parent a13500f commit 4455f60

File tree

1 file changed

+12
-25
lines changed

1 file changed

+12
-25
lines changed

doc/api/fs.md

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,19 +1588,15 @@ then the operation will fail.
15881588

15891589
```js
15901590
const fs = require('fs');
1591+
const { COPYFILE_EXCL } = fs.constants;
15911592

1592-
// destination.txt will be created or overwritten by default.
1593-
fs.copyFile('source.txt', 'destination.txt', (err) => {
1593+
function callback(err) {
15941594
if (err) throw err;
15951595
console.log('source.txt was copied to destination.txt');
1596-
});
1597-
```
1598-
1599-
If the third argument is a number, then it specifies `mode`:
1596+
}
16001597

1601-
```js
1602-
const fs = require('fs');
1603-
const { COPYFILE_EXCL } = fs.constants;
1598+
// destination.txt will be created or overwritten by default.
1599+
fs.copyFile('source.txt', 'destination.txt', callback);
16041600

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

16371633
```js
16381634
const fs = require('fs');
1635+
const { COPYFILE_EXCL } = fs.constants;
16391636

16401637
// destination.txt will be created or overwritten by default.
16411638
fs.copyFileSync('source.txt', 'destination.txt');
16421639
console.log('source.txt was copied to destination.txt');
1643-
```
1644-
1645-
If the third argument is a number, then it specifies `mode`:
1646-
1647-
```js
1648-
const fs = require('fs');
1649-
const { COPYFILE_EXCL } = fs.constants;
16501640

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

47204710
```js
4721-
const fsPromises = require('fs').promises;
4711+
const {
4712+
promises: fsPromises,
4713+
constants: {
4714+
COPYFILE_EXCL
4715+
}
4716+
} = require('fs');
47224717

47234718
// destination.txt will be created or overwritten by default.
47244719
fsPromises.copyFile('source.txt', 'destination.txt')
47254720
.then(() => console.log('source.txt was copied to destination.txt'))
47264721
.catch(() => console.log('The file could not be copied'));
4727-
```
4728-
4729-
If the third argument is a number, then it specifies `mode`:
4730-
4731-
```js
4732-
const fs = require('fs');
4733-
const fsPromises = fs.promises;
4734-
const { COPYFILE_EXCL } = fs.constants;
47354722

47364723
// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.
47374724
fsPromises.copyFile('source.txt', 'destination.txt', COPYFILE_EXCL)

0 commit comments

Comments
 (0)