| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 17a25f1 commit 8907732
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1111,7 +1111,7 @@ try { | |||
| 1111 | 1111 | ||
| 1112 | 1112 | ```cjs | |
| 1113 | 1113 | const { mkdir } = require('node:fs/promises'); | |
| 1114 | - const { resolve, join } = require('node:path'); | ||
| 1114 | + const { join } = require('node:path'); | ||
| 1115 | 1115 | ||
| 1116 | 1116 | async function makeDirectory() { | |
| 1117 | 1117 | const projectFolder = join(__dirname, 'test', 'project'); | |
@@ -1153,9 +1153,11 @@ object with an `encoding` property specifying the character encoding to use. | |||
| 1153 | 1153 | ||
| 1154 | 1154 | ```mjs | |
| 1155 | 1155 | import { mkdtemp } from 'node:fs/promises'; | |
| 1156 | + import { join } from 'node:path'; | ||
| 1157 | + import { tmpdir } from 'node:os'; | ||
| 1156 | 1158 | ||
| 1157 | 1159 | try { | |
| 1158 | - await mkdtemp(path.join(os.tmpdir(), 'foo-')); | ||
| 1160 | + await mkdtemp(join(tmpdir(), 'foo-')); | ||
| 1159 | 1161 | } catch (err) { | |
| 1160 | 1162 | console.error(err); | |
| 1161 | 1163 | } | |
@@ -3220,8 +3222,10 @@ object with an `encoding` property specifying the character encoding to use. | |||
| 3220 | 3222 | ||
| 3221 | 3223 | ```mjs | |
| 3222 | 3224 | import { mkdtemp } from 'node:fs'; | |
| 3225 | + import { join } from 'node:path'; | ||
| 3226 | + import { tmpdir } from 'node:os'; | ||
| 3223 | 3227 | ||
| 3224 | - mkdtemp(path.join(os.tmpdir(), 'foo-'), (err, directory) => { | ||
| 3228 | + mkdtemp(join(tmpdir(), 'foo-'), (err, directory) => { | ||
| 3225 | 3229 | if (err) throw err; | |
| 3226 | 3230 | console.log(directory); | |
| 3227 | 3231 | // 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()` | |||
| 7467 | 7471 | operation might complete before the `fs.rename()` operation: | |
| 7468 | 7472 | ||
| 7469 | 7473 | ```js | |
| 7474 | + const fs = require('node:fs'); | ||
| 7475 | + | ||
| 7470 | 7476 | fs.rename('/tmp/hello', '/tmp/world', (err) => { | |
| 7471 | 7477 | if (err) throw err; | |
| 7472 | 7478 | console.log('renamed complete'); | |
@@ -7483,12 +7489,12 @@ of one before invoking the other: | |||
| 7483 | 7489 | ```mjs | |
| 7484 | 7490 | import { rename, stat } from 'node:fs/promises'; | |
| 7485 | 7491 | ||
| 7486 | - const from = '/tmp/hello'; | ||
| 7487 | - const to = '/tmp/world'; | ||
| 7492 | + const oldPath = '/tmp/hello'; | ||
| 7493 | + const newPath = '/tmp/world'; | ||
| 7488 | 7494 | ||
| 7489 | 7495 | try { | |
| 7490 | - await rename(from, to); | ||
| 7491 | - const stats = await stat(to); | ||
| 7496 | + await rename(oldPath, newPath); | ||
| 7497 | + const stats = await stat(newPath); | ||
| 7492 | 7498 | console.log(`stats: ${JSON.stringify(stats)}`); | |
| 7493 | 7499 | } catch (error) { | |
| 7494 | 7500 | console.error('there was an error:', error.message); | |
@@ -7498,10 +7504,10 @@ try { | |||
| 7498 | 7504 | ```cjs | |
| 7499 | 7505 | const { rename, stat } = require('node:fs/promises'); | |
| 7500 | 7506 | ||
| 7501 | - (async function(from, to) { | ||
| 7507 | + (async function(oldPath, newPath) { | ||
| 7502 | 7508 | try { | |
| 7503 | - await rename(from, to); | ||
| 7504 | - const stats = await stat(to); | ||
| 7509 | + await rename(oldPath, newPath); | ||
| 7510 | + const stats = await stat(newPath); | ||
| 7505 | 7511 | console.log(`stats: ${JSON.stringify(stats)}`); | |
| 7506 | 7512 | } catch (error) { | |
| 7507 | 7513 | console.error('there was an error:', error.message); | |
@@ -7557,7 +7563,7 @@ try { | |||
| 7557 | 7563 | fd = await open('/open/some/file.txt', 'r'); | |
| 7558 | 7564 | // Do something with the file | |
| 7559 | 7565 | } finally { | |
| 7560 | - await fd.close(); | ||
| 7566 | + await fd?.close(); | ||
| 7561 | 7567 | } | |
| 7562 | 7568 | ``` | |
| 7563 | 7569 | ||
@@ -7571,7 +7577,7 @@ try { | |||
| 7571 | 7577 | fd = await open('file.txt', 'r'); | |
| 7572 | 7578 | // Do something with the file | |
| 7573 | 7579 | } finally { | |
| 7574 | - await fd.close(); | ||
| 7580 | + await fd?.close(); | ||
| 7575 | 7581 | } | |
| 7576 | 7582 | ``` | |
| 7577 | 7583 | ||
@@ -7686,7 +7692,7 @@ try { | |||
| 7686 | 7692 | fd = await open(Buffer.from('/open/some/file.txt'), 'r'); | |
| 7687 | 7693 | // Do something with the file | |
| 7688 | 7694 | } finally { | |
| 7689 | - await fd.close(); | ||
| 7695 | + await fd?.close(); | ||
| 7690 | 7696 | } | |
| 7691 | 7697 | ``` | |
| 7692 | 7698 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments