-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12.IMP_trust_issues_with_setTimeOut.js
More file actions
64 lines (46 loc) · 1.62 KB
/
12.IMP_trust_issues_with_setTimeOut.js
File metadata and controls
64 lines (46 loc) · 1.62 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
function cb(){
console.log("Callback");
}
/*
setTimeout with the delay of 5000ms does not always exactly waits for 5 seconds it can take more than 5s and all depends upon the callstack
console.log("Start");
setTimeout(() => {
console.log("TimeOut Callback");
}, 5000);
console.log("End");
..... million of lines of code to executed which takes alot of time let suppose 10s to execute in that case the global execution context will still be busy running the code while the timer is running gets expired and the callback func has already put inside the callback queue
and Event loop constanly monitors whether the callstack is empty or not so as to put the callback func to the callstack
*/
console.log("Start");
setTimeout(() => {
console.log("TimeOut Callback");
}, 5000);
console.log("End");
// Lets block the main thread for 10000 mili seconds;
let startTime=new Date().getTime();
// console.log(startTime);
let endTime=startTime;
while(endTime<startTime+10000){
endTime=new Date().getTime();
}
console.log("while expires");
/*
Output :
Start
End
while expires (after 10 seconds)
TimeOut Callback (after 10 seconds)
*/
// You will see after the 10 seconds the callback function is pushed from the callback queue to the callstack after the global execution context is poped out from the callstack making it empty
// IN THIS WAY THE WHOLE CONCURRENCY MODEL OF JAVASCRIPT WORKS ie we can execute the asynchronous operations in the single threaded language
// console.log("Start");
// setTimeout(() => {
// console.log("TimeOut Callback");
// }, 0);
// console.log("End");
// Output :
/*
Start
End
Timeout Callback
*/