-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5a-timers.js
More file actions
42 lines (39 loc) · 2.34 KB
/
5a-timers.js
File metadata and controls
42 lines (39 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* How to grok when they all come together?
*
* # Timers
* - setInterval(() => {}, 0)
* - next macro task, repeatedly calls after time period
* - setTimeout(() => {}, 1000)
* - next macro task, not before timeout period
* - returns Timeout objects to manipulate afterwards
* - setImmediate(() => {}) / requestAnimationFrame
* - next macro task, but depends on event loop phase
*
* [ref](https://stackoverflow.com/questions/24117267/nodejs-settimeoutfn-0-vs-setimmediatefn/24119936#24119936)
*
* ┌───────────────────────────┐
┌─>│ timers (Timeout/Interval) │
│ └─────────────┬─────────────┘
│ ┌─────────────┴─────────────┐
│ │ pending callbacks │
│ └─────────────┬─────────────┘
│ ┌─────────────┴─────────────┐
│ │ idle, prepare │
│ └─────────────┬─────────────┘ ┌───────────────┐
│ ┌─────────────┴─────────────┐ │ incoming: │
│ │ poll (until check/timers) │<─────┤ connections, │
│ └─────────────┬─────────────┘ │ data, etc. │
│ ┌─────────────┴─────────────┐ └───────────────┘
│ │ check (setImmediate) │
│ └─────────────┬─────────────┘
│ ┌─────────────┴─────────────┐
└──┤ close callbacks │
└───────────────────────────┘
*/
(function timers() {
// comment out below to see the race condition
// setInterval(() => { process.stdout.write("task: setInterval\n")}, 1000);
setTimeout(() => { process.stdout.write("task: setTimeout\n")}, 0);
setImmediate(() => process.stdout.write('task: setImmediate\n'));
})();