-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from GuoXiCheng/dev-c
update file-system md
- Loading branch information
Showing
4 changed files
with
91 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# 文件操作 | ||
|
||
## 文件读取 | ||
|
||
<<< @/../projects/javascript-sandbox/src/file-system/file-operation.ts#read | ||
|
||
## 文件写入 | ||
|
||
<<< @/../projects/javascript-sandbox/src/file-system/file-operation.ts#write | ||
|
||
## 文件追加 | ||
|
||
<<< @/../projects/javascript-sandbox/src/file-system/file-operation.ts#append | ||
|
||
## 文件删除 | ||
|
||
<<< @/../projects/javascript-sandbox/src/file-system/file-operation.ts#unlink |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# 编写形式 | ||
|
||
## 同步形式 | ||
|
||
同步 API 会阻塞主线程和 NodeJS 事件循环直到操作完成。如果出现错误会抛出异常,可以使用 `try...catch` 来捕获异常。 | ||
|
||
通常用于在应用程序启动时加载配置,或者在不影响主线程的场景中使用。 | ||
|
||
```js | ||
const { unlinkSync } = require("node:fs"); | ||
|
||
try { | ||
unlinkSync("/tmp/hello"); | ||
console.log("successfully deleted /tmp/hello"); | ||
} catch (err) { | ||
// handle the error | ||
} | ||
``` | ||
|
||
## 回调形式 | ||
|
||
回调形式不会阻塞主线程和 NodeJS 事件循环,但可能会导致回调地狱。 | ||
|
||
```js | ||
const { unlink } = require("node:fs"); | ||
|
||
unlink("/tmp/hello", (err) => { | ||
if (err) throw err; | ||
console.log("successfully deleted /tmp/hello"); | ||
}); | ||
``` | ||
|
||
## Promise 形式 | ||
|
||
Promise 形式可以避免阻塞主线程和 NodeJS 事件循环,同时避免回调地狱。可以写出近乎同步的代码风格,提升可读性。 | ||
|
||
```js | ||
const { unlink } = require("node:fs/promises"); | ||
|
||
(async function (path) { | ||
try { | ||
await unlink(path); | ||
console.log(`successfully deleted ${path}`); | ||
} catch (error) { | ||
console.error("there was an error:", error.message); | ||
} | ||
})("/tmp/hello"); | ||
``` |