Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit f510bcd

Browse files
xgqfrmswillin
andauthored
docs: Add node-process-nexttick.zh-cn.md (#3219)
* Create node-process-nexttick.zh-cn.md feat: add Chinese translation of Understanding process.nextTick() documentation https://nodejs.dev/zh-cn/learn/understanding-processnexttick/ Signed-off-by: xgqfrms <xgqfrms@outlook.com> * Update node-process-nexttick.zh-cn.md Signed-off-by: Willin 王初瘦 <willin@willin.org> --------- Signed-off-by: xgqfrms <xgqfrms@outlook.com> Signed-off-by: Willin 王初瘦 <willin@willin.org> Co-authored-by: Willin 王初瘦 <willin@willin.org>
1 parent d42fd71 commit f510bcd

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
title: understanding-processnexttick
3+
displayTitle: '理解 process.nextTick()'
4+
description: 'Node.js 中的 process.nextTick 函数以一种特殊的方式与事件循环交互'
5+
authors: xgqfrms, flaviocopes, MylesBorins, LaRuaNa, ahmadawais, ovflowd, marksist300
6+
category: learn
7+
---
8+
9+
当你尝试理解 Node.js 事件循环时,其中一个重要部分是 `process.nextTick()`。每次事件循环完成一次完整的行程,我们称之为一个滴答(`tick`)。
10+
11+
当我们将一个函数传递给 `process.nextTick()` 时,我们指示引擎在当前操作结束时调用此函数,在下一个事件循环滴答开始之前:
12+
13+
```js
14+
process.nextTick(() => {
15+
// do something
16+
});
17+
```
18+
19+
事件循环正忙于处理当前函数代码。当此操作结束时,JS 引擎运行在该操作期间传递给 `nextTick` 调用的所有函数。
20+
21+
这是我们可以告诉 JS 引擎异步地处理函数的方式(在当前函数之后),但要尽快,而不是将其排队。
22+
23+
调用 `setTimeout(() => {}, 0)` 将在下一个滴答结束时执行该函数,比使用 `nextTick()` 时要晚得多,它会优先调用并在下一个滴答开始之前执行它。
24+
25+
当你想确保在下一次事件循环迭代中该代码已被执行时,请使用 `nextTick()`
26+
27+
#### 一个事件顺序的示例
28+
29+
```js
30+
console.log("Hello => number 1");
31+
32+
setTimeout(() => {
33+
console.log("The timeout running last => number 4");
34+
}, 0);
35+
36+
setImmediate(() => {
37+
console.log("Running before the timeout => number 3");
38+
});
39+
40+
process.nextTick(() => {
41+
console.log("Running at next tick => number 2");
42+
});
43+
```
44+
45+
#### 输出
46+
47+
```bash
48+
Hello => number 1
49+
Running at next tick => number 2
50+
The timeout running last => number 4
51+
Running before the timeout => number 3
52+
```

0 commit comments

Comments
 (0)