mirror of
https://github.com/nodejs/node.git
synced 2026-11-18 18:15:17 -05:00
doc: improve fs code example quality
PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
1 parent
ab8942848b
commit
070e773abf
1 file changed
+19
-13
+19
-13
@@ -1117,7 +1117,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');
|
||||
@@ -1159,9 +1159,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);
|
||||
}
|
||||
@@ -3237,8 +3239,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
|
||||
@@ -7542,6 +7546,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');
|
||||
@@ -7558,12 +7564,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);
|
||||
@@ -7573,10 +7579,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);
|
||||
@@ -7632,7 +7638,7 @@ try {
|
||||
fd = await open('/open/some/file.txt', 'r');
|
||||
// Do something with the file
|
||||
} finally {
|
||||
await fd.close();
|
||||
await fd?.close();
|
||||
}
|
||||
```
|
||||
|
||||
@@ -7646,7 +7652,7 @@ try {
|
||||
fd = await open('file.txt', 'r');
|
||||
// Do something with the file
|
||||
} finally {
|
||||
await fd.close();
|
||||
await fd?.close();
|
||||
}
|
||||
```
|
||||
|
||||
@@ -7761,7 +7767,7 @@ try {
|
||||
fd = await open(Buffer.from('/open/some/file.txt'), 'r');
|
||||
// Do something with the file
|
||||
} finally {
|
||||
await fd.close();
|
||||
await fd?.close();
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Reference in new issue
Block a user