let a = 1; let b = 2; let c = a + b; console.log(c)
One of the biggest issues with synchronous code comes when you have to call a function that requires a certain amount of time to be solved. The next operation needs to wait even if the result of the previous operation is not required. Think of this situation in big applications, the amount of time required to run a script is very long. Loading times can be slower with synchronous programming.
The heap: responsible for storing our data. It is used to allocate our variables and objects
The call stack: Used to keep track of multiple function calls. Think of it as a stack of dishes that pile up, and when it comes time to wash them, the best way to do it is to take the dishes from the top until you get to the last one. If it sees an API call it sends it to the Web API container
The callback queue: here will be receiving the callbacks from the Web API container. It works in a way that the first callback received is the first one sent to the call stack to be executed.
Event loop: a mechanism that manages the order of execution. It constantly checks the call stack and the callback queue. So when the call stack is empty the event loop immediately passes the callbacks in the queue for its execution.
3.Asynchronous functions exposed by the Web API extend the language
Now we can see that even though there is the main thread in which all the code we write in our programs is executed (call stack) there are asynchronous functions exposed by the Web API in the browser that perform some operations (fetch data, set local storage, dom manipulation) that do not block the main thread simply because they are not performed in that thread ( waiting on the callback queue).
Important to realize that the concept of more than one thing happening at the same time, or multiple related things happening without waiting for the previous one to be completed is the base of the term Asynchronous.
Thanks for reading and if you have any suggestions please don't hesitate to leave a comment, I will be happy to take into account your recommendations.
I'll also leave bellow some links where you can find more info:
Top comments (0)