-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay17.js
More file actions
84 lines (65 loc) · 2.25 KB
/
Day17.js
File metadata and controls
84 lines (65 loc) · 2.25 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// EP-17 | TRUST ISSUES with setTimeout()
// A setTimeout with a specified delay does not always wait for the exact specified delay.
// It does not when the callback will invoke if delay is 5 sec the callback can take 5, 6, 7 etc
// It all depends on the call stack
console.log("start");
setTimeout(function cb() {
console.log("callback");
}, 5000);
console.log("end");
// Millions of lines of code taking 10 sec to execute
// The Global execution context will still be busy in executing these lines and the setTimeout callback will
// still wait inside the callback queue until the Global execution context gets popped out from the call
// stack.
// Now the event loop is constantly checking whether the call stack is empty or not which is a needed cond.
// to send the callback inside the call stack. This callback will starve inside the callback queue.
// This setTimeout callback will get the chance to execute once the call stack gets empty. This is also known
// as the concurrency model in the JS.
// So, eventually the setTimeout will take more time than the specified delay, that's why we cannot trust
// setTimeout.
// Example:
console.log("start");
setTimeout(function cb() {
console.log("callback");
}, 5000);
console.log("end");
// Millions of line of code (Blocking the main thread)
let startTime = new Date().getTime();
let endTime = startTime;
while (endTime < startTime + 10000) {
endTime = new Date().getTime();
}
console.log("while expires");
// Output:
// start
// end
// while expires
// callback
// When while expires printed the JS engine will quickly execute the callback.
// So, the correct definition for the delay is like that the setTimeout will at-least wait for the entered
// delay, it can take more time but will at-least wait for the desired time.
console.log("start");
setTimeout(function cb() {
console.log("callback");
}, 0);
console.log("end");
// Output:
// start
// end
// callback
// The callback will still moves to the callback queue and wait for the call stack to get empty, due to
// which 'callback' will print in last line.
console.log("start");
function cb() {
console.log("callback");
}
cb();
setTimeout(function cb() {
console.log("callback");
}, 0);
console.log("end");
// Output:
// start
// callback
// end
// callback