Asynchronous NodeJS

운명애·2021년 2월 12일
0

JS

목록 보기
4/4

Call Stack is a simple data structure provided by the V8 JavaScript engine.

The job of the call stack is to track the execution of our program and it does that by keeping track of all of the functions that are currently running.

Frist, our script is going to get wrapped in that main function that node.js provides.

setTimeout is not a part of the js programming language.
v8 also has no implementation for it.
Instead, it is nodeJS which creates an implementation of setTimeout using C++ and provides it to our script. It is an asynchronous way to wait a specific amount of time, and then have the function run.

When we call setTimeout, it's really registering an event with nodeJS APIs.
That is an event callback pair where the event is simply to wait a specific amount of time and the callback has the function run.

When we call setTimeout, a new event gets registered in node APIs(callback pair) and wating for the amount of time.
At the point where the event got registered, the clock starts ticking down.

While we are wating for those times, we can actually do other stuff inside of the call stack.

JavaScript itself is a single threaded programming. You can do one thing at a time and the call stack enforces that. We can only have one function on the top of the call stack is the thing we're doing.
There's no way to execute two things at the same time.

But, that doesn't mean nodeJS is compeletly single threaded. The code you run is indeed still single threaded, but node uses other threads in C++ behind the scenes to manage your events.

That's what allows us to continue running our application while we're wating the amount of time. We don't have to compeletly wait and this is the non blocking nature of node. This is not blocking the rest of the app from running!

When the specific amount of time has passed, the callback (node APIs) needs to get executed.

This is where the callback queue and the event loop come into play.

The job of the callbacke queue is to maintain a list of all the callback functions that are ready to get executed.
When a given event is ready to get executed in the callback, the callback function defined in setTimeout is gonna get added on to the callback queue which is just a standard line.

So, we have this callback and it's ready to get executed but before it can be executed, it needs to be added on to the call stack. That's where the function to go run.

This is where the event loop comes into play. The event loop looks at two things. It looks at the call stack and the callback queue. If the call stack is empty, it's going to run items from the callback queue.

Even if the event loop knows the callback function in the callback queue is ready to get executed, if the call stack is not empty, the event loop can not add the callback function onto the call stack. This is why the callback function isn't run right after even the time that set has elapsed.

The event loop needs to wait for the call stack to be empty.

With regular synchronous scripts, when the main( ) got removed, in other words, the call stack becomes empty, it signifies the end of the application.

That is not the case with an asynchronous program. Right after the call stack being empty, the event loop can start to add the callback function in the callback queue onto the call stack!

profile
개발자 하고 싶어요..

0개의 댓글