while (queue.waitForMessage()) {
queue.processNextMessage();
}
Let’s check the code above.
Assuming the ‘waitForMessage’ function behaves synchronously. It means it runs an infinite loop and waits for a message, and if there is a message, process the next one.
In other words, the Event Loop
is not a component of the JavaScript
engine, but manages which job to pile up on the call stack in the running environment (Browser or runtime environment such as Node.js
).
Here is a brief explanation of how the event loop works.
setTimeout
creates a timer event, waits for the amount from the parameter of the time passed, and fires it.
If there are no arguments, it returns a default value, 0. It should run immediately because the timer is set to zero, but it’s not.
Take a look at how the code flows one by one and see why.
Promise
is the API
mainly applied for future completion or failure situations with asynchronous operations.
In the code above, it returns the Promise
through the resolve
method and it passes the callback through the then
method.
This code works in the following order.
setTimeout
.task
queue.Promise
.then
as a callback is placed in the Micro task
queue.Let’s take a quick look at ‘Microtask’ here.
In ES2015
, API
such as Promise
has been added for handling concurrency. It handles Micro tasks
, slightly different from regular tasks.
Tasks are jobs that should be executed sequentially in a browser or other operating environment.
The task is simply to execute a script or callback caused by the occurrence such as setTimeout
and UI event.
Micro tasks are asynchronous tasks that must be executed immediately after the currently running task. In other words, microtasks have a higher priority than regular tasks. It includes Observer API
, process.nextTick
in NodeJS
and Promise
, used in the example.
If the Microtask concept is included in the sequence of the event loop described above, code flow is like this.
Returning to the example code, finally, understand what the event loop is doing.
The callback passed from the then
method of Promise
is a microtask, executed after entering the call-stack into by event loop.
After that, it prints out hello
.
So, can solve all the problems by utilizing the asynchronous web API
with your understanding of the event loop? unfortunately not.
However, the possibility still remains that the execution of the next task will be blocked by the predecessor task.
In the example below, the code is executed one after the other. At first, the ‘someExpensive’ function, an expensive operation, is pushed onto the call stack.
Because of this, printing ‘hello’ is blocked from the event loop. It can only be run after the job is complete.
In summary, tasks are always executed sequentially through the event loop, other tasks cannot run until certain tasks are completed. Microtasks queues have higher priority than regular task queues. UI events cannot be fired until all microtask queues are empty.
This means that events directly connected to the UI such as clicks, text input, and rendering can be blocked when tasks or microtasks that involve costly calculations run on the CPU. This can be a core issue to lower the user experience.
SetTimeout()
function is disappearing from the call stack in javascript first, then re-piles on the stack when the call stack is empty. This process is concurrency with the Event Loop.WepAPIs
(DOM
, Ajax
, SetTimeout
) to effectively support threads that can be called from javascript.SetTimeout
function is a separate API that does not exist in JavaScript or in the browser’s V8 engine source. It is in JavaScript’s runtime environment.SetTimeout
is minimal time and can be more.Event loop
is to watch the call stack and task queue, and the first callback in the queue is piled on the stack when the stack is empty.Request
of Fetch API
is a similar processing method just like the Event loop (to process the stack after requesting and waiting outside the stack until stack is empty)addEventListener
is a structure that sent from the Web API
to the callback queue if an event occurs. It is sequentially stacked on the call stack then executed.