- Server side Applications: Node Js , Deno Js ...
- Native Mobile Applications: React Native , Ionic ...
- Native Desktop Applications: Electron Js , NW Js ...
- Browser Games: Phaser Js, Create Js ...
- Smartwatch Applicatons: Pebble Js ...
- Machine Learning: Tenserflow Js ...
Note:- The runtime environment in a browser is very different from that of Node.js. But, most of the following concepts are still relevant.
In the context of the browser, this is comprised of the following elements:
- Web APIs
- The callback queue
- The event loop
For the purpose of this article, we are going to focus on the V8 engine.
- When a script calls a function, the interpreter adds it to the call stack and then starts carrying out the function.
- Any functions that are called by that function are added to the call stack further up and run where their calls are reached.
- When the current function is finished, the interpreter takes it off the stack and resumes executions where it left off in the last code listing.
- If the stack takes up more space than it has been assigned to it, it results in "Stack Overflow" error.
The Callback Queue
Callback queue or message queue contains the list of messages to be processed and their associated call-back functions. The messages are queued in response to external events ( Like response from the click event ). As the external events are web APIs that are not part of the V8 runtime, when the call stack encounters it pushes to another block where it starts to execute and pushes to callback queue when it receives the response or the timer is finished.
Event loop is a simple piece that puts the whole puzzle together. So when the set timeout or click function is called or when pushed onto the stack, after the execution it goes to the callback queue. if the call stack is empty, then it pushes the first processed callback function present in the callback queue to the call stack. Each message is processed completely before any other message is processed.
Did you find this article valuable?
Support Shubham Raj by becoming a sponsor. Any amount is appreciated!