Skip to content

Commit ba5ff15

Browse files
jakecastellitargos
authored andcommitted
doc: improve fs code example quality
PR-URL: #46948 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: LiviaMedeiros <livia@cirno.name> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com> Reviewed-By: Qingyu Deng <i@ayase-lab.com>
1 parent 6f18b94 commit ba5ff15

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

doc/api/fs.md

+19-13
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,7 @@ try {
11171117
11181118
```cjs
11191119
const { mkdir } = require('node:fs/promises');
1120-
const { resolve, join } = require('node:path');
1120+
const { join } = require('node:path');
11211121

11221122
async function makeDirectory() {
11231123
const projectFolder = join(__dirname, 'test', 'project');
@@ -1159,9 +1159,11 @@ object with an `encoding` property specifying the character encoding to use.
11591159
11601160
```mjs
11611161
import { mkdtemp } from 'node:fs/promises';
1162+
import { join } from 'node:path';
1163+
import { tmpdir } from 'node:os';
11621164

11631165
try {
1164-
await mkdtemp(path.join(os.tmpdir(), 'foo-'));
1166+
await mkdtemp(join(tmpdir(), 'foo-'));
11651167
} catch (err) {
11661168
console.error(err);
11671169
}
@@ -3237,8 +3239,10 @@ object with an `encoding` property specifying the character encoding to use.
32373239
32383240
```mjs
32393241
import { mkdtemp } from 'node:fs';
3242+
import { join } from 'node:path';
3243+
import { tmpdir } from 'node:os';
32403244
3241-
mkdtemp(path.join(os.tmpdir(), 'foo-'), (err, directory) => {
3245+
mkdtemp(join(tmpdir(), 'foo-'), (err, directory) => {
32423246
if (err) throw err;
32433247
console.log(directory);
32443248
// Prints: /tmp/foo-itXde2 or C:\Users\...\AppData\Local\Temp\foo-itXde2
@@ -7542,6 +7546,8 @@ For example, the following is prone to error because the `fs.stat()`
75427546
operation might complete before the `fs.rename()` operation:
75437547
75447548
```js
7549+
const fs = require('node:fs');
7550+
75457551
fs.rename('/tmp/hello', '/tmp/world', (err) => {
75467552
if (err) throw err;
75477553
console.log('renamed complete');
@@ -7558,12 +7564,12 @@ of one before invoking the other:
75587564
```mjs
75597565
import { rename, stat } from 'node:fs/promises';
75607566
7561-
const from = '/tmp/hello';
7562-
const to = '/tmp/world';
7567+
const oldPath = '/tmp/hello';
7568+
const newPath = '/tmp/world';
75637569
75647570
try {
7565-
await rename(from, to);
7566-
const stats = await stat(to);
7571+
await rename(oldPath, newPath);
7572+
const stats = await stat(newPath);
75677573
console.log(`stats: ${JSON.stringify(stats)}`);
75687574
} catch (error) {
75697575
console.error('there was an error:', error.message);
@@ -7573,10 +7579,10 @@ try {
75737579
```cjs
75747580
const { rename, stat } = require('node:fs/promises');
75757581
7576-
(async function(from, to) {
7582+
(async function(oldPath, newPath) {
75777583
try {
7578-
await rename(from, to);
7579-
const stats = await stat(to);
7584+
await rename(oldPath, newPath);
7585+
const stats = await stat(newPath);
75807586
console.log(`stats: ${JSON.stringify(stats)}`);
75817587
} catch (error) {
75827588
console.error('there was an error:', error.message);
@@ -7632,7 +7638,7 @@ try {
76327638
fd = await open('/open/some/file.txt', 'r');
76337639
// Do something with the file
76347640
} finally {
7635-
await fd.close();
7641+
await fd?.close();
76367642
}
76377643
```
76387644
@@ -7646,7 +7652,7 @@ try {
76467652
fd = await open('file.txt', 'r');
76477653
// Do something with the file
76487654
} finally {
7649-
await fd.close();
7655+
await fd?.close();
76507656
}
76517657
```
76527658
@@ -7761,7 +7767,7 @@ try {
77617767
fd = await open(Buffer.from('/open/some/file.txt'), 'r');
77627768
// Do something with the file
77637769
} finally {
7764-
await fd.close();
7770+
await fd?.close();
77657771
}
77667772
```
77677773

0 commit comments

Comments
 (0)