*Isn't that counter-intuitive? * How can something be single-threaded and asynchronous as well?
Well, there are a lot of such things in JS and to understand it all, you need a deep dive into JS and its working.
You would have heard ES6, ES7 while working with JS, the ES here stands for ECMAScript.
ECMA puts a lot of specifications in place related to notations, operations, syntax, functions, classes etc.
You can refer to all these specifications here: ECMAScript
But ECMA doesn't have any documentation on the event loop, non-blocking, asynchronous JS.
Hmm, so where do we go now? We go to the JS engines!!
- V8 ( Developed by Google | Used in chrome, Node.js runtime, Deno runtime)
- Spider Monkey (Developed by Mozilla | Used in Firefox)
- WebKit (Developed by Apple | Used in Safari)
- Chakra (Developed by Microsoft | Used in Internet Explorer)
Check out the list of all JS engines here: List of ECMAScript engines (Wikipedia)
The V8 engine comes with two main components which are
- Call stack: This is where all the execution happens.
- Heap: This is where the memory allocation happens.
But even standalone V8 doesn't make JS asynchronous.
So, how does it become asynchronous? Keep Reading.
So, let us try to understand the single-threaded nature of JS first and what does it mean. Then we will see how does it become asynchronous and what problem does it solve.
Let us get this single thread concept right!
One Thread == One Call Stack == One Thing at a Time
This is called the synchronous execution of code.
Now as you understood this, let us try a few programs with the main thread to understand the call stack and the problem with it.
Continue Reading the Blog Here