Javascript Event Loop Flashcards
What are the elements of the javascript event loop?
Javascript Engine (Heap & Call Stack)
Web APIs
Task Queue
MicroTask Queue
Event Loop itself
What are the two structures inside the javascript engine?
Heap & Call Stack
What are elements of the web api?
fetch
setTimeout
localStorage
sessionStorage
HTMLDivElement
indexedDB
Which structure holds async callbacks?
The task queue, or callback queue.
What is the job of the eventloop?
The event loops checks the call stack. If the call stack is empty and there is no more code to execute, meaning all synchronous tasks are done, then the event loop pops the next callback off the task queue and onto the call stack.
What does the delay signify in setTimeout?
The delay signifies when the callback will be added to the task queue.
Which structure do promises use?
Promises use the micro task queue. This is because they use the queueMicroTask function under the hood.
How is the microTaskQueue prioritized?
The microTask queue will be fully emptied and executed when the call stack is empty before anything from the task queue is added to the call stack.
The microTask queue is also checked each time the task queue is popped.
What is the console output of this code:
Promise.resolve().then(() => console.log(1));
setTimeout(() => console.log(2), 10);
queueMicrotask(() => {
console.log(3);
queueMicrotask(() => console.log(4));
});
console.log(5);
5
1
3
4
2
What is the event loop?
The event loop is a piece of code that orchestates and ties together various aspects of the Javascript v8 engine and various browser apis.
How is more code to be executed represented on the call stack?
It is represented as an anonymous function.