Skip to content

Commit

Permalink
doc: improve fs code example quality
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
jakecastelli authored and danielleadams committed Apr 11, 2023
1 parent 17a25f1 commit 8907732
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ try {
```cjs
const { mkdir } = require('node:fs/promises');
const { resolve, join } = require('node:path');
const { join } = require('node:path');

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

try {
await mkdtemp(path.join(os.tmpdir(), 'foo-'));
await mkdtemp(join(tmpdir(), 'foo-'));
} catch (err) {
console.error(err);
}
Expand Down Expand Up @@ -3220,8 +3222,10 @@ object with an `encoding` property specifying the character encoding to use.
```mjs
import { mkdtemp } from 'node:fs';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
mkdtemp(path.join(os.tmpdir(), 'foo-'), (err, directory) => {
mkdtemp(join(tmpdir(), 'foo-'), (err, directory) => {
if (err) throw err;
console.log(directory);
// Prints: /tmp/foo-itXde2 or C:\Users\...\AppData\Local\Temp\foo-itXde2
Expand Down Expand Up @@ -7467,6 +7471,8 @@ For example, the following is prone to error because the `fs.stat()`
operation might complete before the `fs.rename()` operation:
```js
const fs = require('node:fs');
fs.rename('/tmp/hello', '/tmp/world', (err) => {
if (err) throw err;
console.log('renamed complete');
Expand All @@ -7483,12 +7489,12 @@ of one before invoking the other:
```mjs
import { rename, stat } from 'node:fs/promises';
const from = '/tmp/hello';
const to = '/tmp/world';
const oldPath = '/tmp/hello';
const newPath = '/tmp/world';
try {
await rename(from, to);
const stats = await stat(to);
await rename(oldPath, newPath);
const stats = await stat(newPath);
console.log(`stats: ${JSON.stringify(stats)}`);
} catch (error) {
console.error('there was an error:', error.message);
Expand All @@ -7498,10 +7504,10 @@ try {
```cjs
const { rename, stat } = require('node:fs/promises');
(async function(from, to) {
(async function(oldPath, newPath) {
try {
await rename(from, to);
const stats = await stat(to);
await rename(oldPath, newPath);
const stats = await stat(newPath);
console.log(`stats: ${JSON.stringify(stats)}`);
} catch (error) {
console.error('there was an error:', error.message);
Expand Down Expand Up @@ -7557,7 +7563,7 @@ try {
fd = await open('/open/some/file.txt', 'r');
// Do something with the file
} finally {
await fd.close();
await fd?.close();
}
```
Expand All @@ -7571,7 +7577,7 @@ try {
fd = await open('file.txt', 'r');
// Do something with the file
} finally {
await fd.close();
await fd?.close();
}
```
Expand Down Expand Up @@ -7686,7 +7692,7 @@ try {
fd = await open(Buffer.from('/open/some/file.txt'), 'r');
// Do something with the file
} finally {
await fd.close();
await fd?.close();
}
```
Expand Down

0 comments on commit 8907732

Please sign in to comment.