Skip to content

Commit 8907732

Browse files
jakecastellidanielleadams
authored andcommitted
doc: improve fs code example quality
PR-URL: #46948 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: LiviaMedeiros <[email protected]> Reviewed-By: Mohammed Keyvanzadeh <[email protected]> Reviewed-By: Qingyu Deng <[email protected]>
1 parent 17a25f1 commit 8907732

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
@@ -1111,7 +1111,7 @@ try {
11111111
11121112
```cjs
11131113
const { mkdir } = require('node:fs/promises');
1114-
const { resolve, join } = require('node:path');
1114+
const { join } = require('node:path');
11151115

11161116
async function makeDirectory() {
11171117
const projectFolder = join(__dirname, 'test', 'project');
@@ -1153,9 +1153,11 @@ object with an `encoding` property specifying the character encoding to use.
11531153
11541154
```mjs
11551155
import { mkdtemp } from 'node:fs/promises';
1156+
import { join } from 'node:path';
1157+
import { tmpdir } from 'node:os';
11561158

11571159
try {
1158-
await mkdtemp(path.join(os.tmpdir(), 'foo-'));
1160+
await mkdtemp(join(tmpdir(), 'foo-'));
11591161
} catch (err) {
11601162
console.error(err);
11611163
}
@@ -3220,8 +3222,10 @@ object with an `encoding` property specifying the character encoding to use.
32203222
32213223
```mjs
32223224
import { mkdtemp } from 'node:fs';
3225+
import { join } from 'node:path';
3226+
import { tmpdir } from 'node:os';
32233227
3224-
mkdtemp(path.join(os.tmpdir(), 'foo-'), (err, directory) => {
3228+
mkdtemp(join(tmpdir(), 'foo-'), (err, directory) => {
32253229
if (err) throw err;
32263230
console.log(directory);
32273231
// Prints: /tmp/foo-itXde2 or C:\Users\...\AppData\Local\Temp\foo-itXde2
@@ -7467,6 +7471,8 @@ For example, the following is prone to error because the `fs.stat()`
74677471
operation might complete before the `fs.rename()` operation:
74687472
74697473
```js
7474+
const fs = require('node:fs');
7475+
74707476
fs.rename('/tmp/hello', '/tmp/world', (err) => {
74717477
if (err) throw err;
74727478
console.log('renamed complete');
@@ -7483,12 +7489,12 @@ of one before invoking the other:
74837489
```mjs
74847490
import { rename, stat } from 'node:fs/promises';
74857491
7486-
const from = '/tmp/hello';
7487-
const to = '/tmp/world';
7492+
const oldPath = '/tmp/hello';
7493+
const newPath = '/tmp/world';
74887494
74897495
try {
7490-
await rename(from, to);
7491-
const stats = await stat(to);
7496+
await rename(oldPath, newPath);
7497+
const stats = await stat(newPath);
74927498
console.log(`stats: ${JSON.stringify(stats)}`);
74937499
} catch (error) {
74947500
console.error('there was an error:', error.message);
@@ -7498,10 +7504,10 @@ try {
74987504
```cjs
74997505
const { rename, stat } = require('node:fs/promises');
75007506
7501-
(async function(from, to) {
7507+
(async function(oldPath, newPath) {
75027508
try {
7503-
await rename(from, to);
7504-
const stats = await stat(to);
7509+
await rename(oldPath, newPath);
7510+
const stats = await stat(newPath);
75057511
console.log(`stats: ${JSON.stringify(stats)}`);
75067512
} catch (error) {
75077513
console.error('there was an error:', error.message);
@@ -7557,7 +7563,7 @@ try {
75577563
fd = await open('/open/some/file.txt', 'r');
75587564
// Do something with the file
75597565
} finally {
7560-
await fd.close();
7566+
await fd?.close();
75617567
}
75627568
```
75637569
@@ -7571,7 +7577,7 @@ try {
75717577
fd = await open('file.txt', 'r');
75727578
// Do something with the file
75737579
} finally {
7574-
await fd.close();
7580+
await fd?.close();
75757581
}
75767582
```
75777583
@@ -7686,7 +7692,7 @@ try {
76867692
fd = await open(Buffer.from('/open/some/file.txt'), 'r');
76877693
// Do something with the file
76887694
} finally {
7689-
await fd.close();
7695+
await fd?.close();
76907696
}
76917697
```
76927698

0 commit comments

Comments
 (0)