示例工程 08-event-loop-concurrency
08-event-loop-concurrency/01-ordering-quiz/README.md
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
# 01 · 输出顺序题
**演示**:同步代码、`queueMicrotask`、`Promise.then`、`async/await` 续体、`setTimeout` 的完整执行顺序;嵌套微任务的插入位置。
**怎么看**:`node scripts/serve.mjs` 后打开 <http://localhost:8080/examples/08-event-loop-concurrency/01-ordering-quiz/>,开 Console。
**预期输出**:
```text
== 实际顺序 ==
1. 1 sync-start
2. 5 async-start
3. 10 sync-end
4. 3 queueMicrotask
5. 4 promise-then
6. 6 after-await
7. 7 then-a
8. 8 nested-microtask
9. 9 then-b
10. 2 timeout
== 结论 ==
同步代码先跑完;然后清空微任务队列(含 await 续体与嵌套微任务);最后才执行 setTimeout。
```
**注意**:`await null` 的续体在 `Promise.resolve().then(...)` 之后入队,但早于后面那个 `.then` 链的第一环,所以 `6 after-await` 排在 `7 then-a` 之前。`8 nested-microtask` 排在 `9 then-b` 之前——嵌套微任务在本轮清空时先于「下一个 then」。需 `http://` 协议。