JavaScript Event Loop
- 5. I have a CALL
STACK,
an EVENT LOOP,
a CALLBACK
QUEUE,
some others APIs,
- 9. do you have a CALL
STACK,
an EVENT LOOP,
a CALLBACK QUEUE,
some others APIs,
and stuffs ???
HEY
,
- 10. I have a CALL
STACK and a
HEAP
…
I don’t know
what other
- 14. function multiply(a, b){
return a * b;
}
function calcSquare(n){
return multiply(n, n);
}
function printSquare(n){
var result = calcSquare(n);
console.log(result);
}
printSquare(4);
THE CALL STACK
- 15. main()
function multiply(a, b){
return b * b;
}
function calcSquare(n){
return multiply(n, n);
}
function printSquare(n){
var result = calcSquare(n);
console.log(result);
}
printSquare(4);
THE CALL STACK
- 16. main()
function multiply(a, b){
return b * b;
}
function calcSquare(n){
return multiply(n, n);
}
function printSquare(n){
var result = calcSquare(n);
console.log(result);
}
printSquare(4);
THE CALL STACK
- 17. main()
function multiply(a, b){
return b * b;
}
function calcSquare(n){
return multiply(n, n);
}
function printSquare(n){
var result = calcSquare(n);
console.log(result);
}
printSquare(4);
THE CALL STACK
- 18. main()
function multiply(a, b){
return b * b;
}
function calcSquare(n){
return multiply(n, n);
}
function printSquare(n){
var result = calcSquare(n);
console.log(result);
}
printSquare(4);
THE CALL STACK
- 19. main()
function multiply(a, b){
return b * b;
}
function calcSquare(n){
return multiply(n, n);
}
function printSquare(n){
var result = calcSquare(n);
console.log(result);
}
printSquare(4);
THE CALL STACK
printSquare(n)
- 20. main()
function multiply(a, b){
return b * b;
}
function calcSquare(n){
return multiply(n, n);
}
function printSquare(n){
var result = calcSquare(n);
console.log(result);
}
printSquare(4);
THE CALL STACK
printSquare(n)
calcSquare(n)
- 21. main()
function multiply(a, b){
return b * b;
}
function calcSquare(n){
return multiply(n, n);
}
function printSquare(n){
var result = calcSquare(n);
console.log(result);
}
printSquare(4);
THE CALL STACK
printSquare(n)
calcSquare(n)
multiply(n, n)
- 22. main()
function multiply(a, b){
return b * b;
}
function calcSquare(n){
return multiply(n, n);
}
function printSquare(n){
var result = calcSquare(n);
console.log(result);
}
printSquare(4);
THE CALL STACK
printSquare(n)
calcSquare(n)
- 23. main()
function multiply(a, b){
return b * b;
}
function calcSquare(n){
return multiply(n, n);
}
function printSquare(n){
var result = calcSquare(n);
console.log(result);
}
printSquare(4);
THE CALL STACK
printSquare(n)
- 24. main()
function multiply(a, b){
return b * b;
}
function calcSquare(n){
return multiply(n, n);
}
function printSquare(n){
var result = calcSquare(n);
console.log(result);
}
printSquare(4);
THE CALL STACK
printSquare(n)
console.log()
- 25. main()
function multiply(a, b){
return b * b;
}
function calcSquare(n){
return multiply(n, n);
}
function printSquare(n){
var result = calcSquare(n);
console.log(result);
}
printSquare(4);
THE CALL STACK
printSquare(n)
- 26. main()
function multiply(a, b){
return b * b;
}
function calcSquare(n){
return multiply(n, n);
}
function printSquare(n){
var result = calcSquare(n);
console.log(result);
}
printSquare(4);
THE CALL STACK
- 27. function multiply(a, b){
return b * b;
}
function calcSquare(n){
return multiply(n, n);
}
function printSquare(n){
var result = calcSquare(n);
console.log(result);
}
printSquare(4);
THE CALL STACK
- 28. function one(){
throw new Error(“Oops!”);
}
function two(){
one();
}
function three(){
two();
}
three();
THE CALL STACK
main()
three(n)
two()
one()
- 37. var a = $.getSynchronous(“/a”);
var b = $.getSynchronous(“/b”);
var c = $.getSynchronous(“/c”);
// get data done, now log them out
console.log(a);
console.log(b);
console.log(c);
THE CALL STACK
main()
- 38. var a = $.getSynchronous(“/a”);
var b = $.getSynchronous(“/b”);
var c = $.getSynchronous(“/c”);
// get data done, now log them out
console.log(a);
console.log(b);
console.log(c);
THE CALL STACK
main()
getSynchronous(“/a”)
- 39. var a = $.getSynchronous(“/a”);
var b = $.getSynchronous(“/b”);
var c = $.getSynchronous(“/c”);
// get data done, now log them out
console.log(a);
console.log(b);
console.log(c);
THE CALL STACK
main()
getSynchronous(“/b”)
- 40. var a = $.getSynchronous(“/a”);
var b = $.getSynchronous(“/b”);
var c = $.getSynchronous(“/c”);
// get data done, now log them out
console.log(a);
console.log(b);
console.log(c);
THE CALL STACK
main()
getSynchronous(“/c”)
- 41. var a = $.getSynchronous(“/a”);
var b = $.getSynchronous(“/b”);
var c = $.getSynchronous(“/c”);
// get data done, now log them out
console.log(a);
console.log(b);
console.log(c);
THE CALL STACK
main()
console.log(a)
- 42. var a = $.getSynchronous(“/a”);
var b = $.getSynchronous(“/b”);
var c = $.getSynchronous(“/c”);
// get data done, now log them out
console.log(a);
console.log(b);
console.log(c);
THE CALL STACK
main()
console.log(b)
- 43. var a = $.getSynchronous(“/a”);
var b = $.getSynchronous(“/b”);
var c = $.getSynchronous(“/c”);
// get data done, now log them out
console.log(a);
console.log(b);
console.log(c);
THE CALL STACK
main()
console.log(c)
- 44. WHY IS THIS
A PROBLEM ?
Because we runs code in
BROWSERS.
See the demo.
- 65. WHAT DOES
DO ?
-It watches the Call Stack and the Callback Queue
-If the Stack is empty, it takes the first element in
the Callback Queue, and pushes it into the Stack
EVEN
T
LOOP
- 70. Beside setTimeout, the other
web APIs are (for
examples):
- DOM manipulation
- XHR (XMLHttpRequest)
- Etc…