-
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 #77 from GuoXiCheng/dev-c
add event-loop md
- Loading branch information
Showing
3 changed files
with
44 additions
and
5 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
31 changes: 31 additions & 0 deletions
31
src/javascript/ecma-script/javascript-engine/event-loop.md
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,31 @@ | ||
# 事件循环 | ||
|
||
## 什么是事件循环 | ||
|
||
事件循环机制是 JavaScript 实现非阻塞异步编程的核心原理。 | ||
|
||
## 事件循环的执行过程 | ||
|
||
::: details 参考代码 | ||
|
||
```js | ||
console.log("Test Start"); | ||
setTimeout(() => console.log("setTimeout"), 0); | ||
Promise.resolve("Promise").then((res) => console.log(res)); | ||
console.log("Test End"); | ||
|
||
// Test Start | ||
// Test End | ||
// Promise | ||
// setTimeout | ||
``` | ||
|
||
::: | ||
|
||
首先以同步方式执行代码,然后事件循环将检查微任务队列。 | ||
|
||
如果微任务队列不为空,则依次执行队列中的任务,直到队列被清空。 | ||
|
||
微任务队列中的任务清空后,事件循环会从宏任务队列中取出一个任务执行。 | ||
|
||
每执行完一个宏任务,事件循环会再次检查微任务队列,重复上述过程。 |