# Run, JavaScript, Run

> JavaScript is a single-threaded non-blocking programming language. I have heard this since I started coding, without fully understanding what it means. By looking into the Event Loop Model and the notion of tasks, the idea is to better understand those notions.

Source: https://antistatique.net/en/blog/run-javascript-run

Category: Development

# Run, JavaScript, Run

Author(s)

[

![Iris](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2020/12/Iris-300x300.jpg)



](#)

[Iris](#)18 April 2023

JavaScript is a **single-threaded non-blocking** programming language. I have heard this since I started coding, without fully understanding what it means. By looking into the __Event_ _Loop_ Model_ and the notion of _tasks_, the idea is to better understand those notions.

## The Event Loop Model

An application or website coded in JavaScript runs on one unique thread, sharing its Event Loop. The Event Loop is responsible for scheduling and executing the different tasks (= piece of code) within its thread and is composed of three elements:

-   The **Call Stack** (or Execution Stack) which handles the synchronous calls and executes the code one at a time.
-   The **Web APIs** are asynchronous calls which are triggered by an action (for example, on a click, or when a timer ends)
-   The **Callback Queue** which handles the asynchronous calls and can be divided into two different queues, the Job Queue and the Task Queue.

![The Event Loop schema](https://images.statique.ch/t/w_750,f_jpeg,q_70/wp/app/uploads/2023/06/eventloop.png)

Source: [https://programs.team/javascript-macro-task-micro-task.html](https://programs.team/javascript-macro-task-micro-task.html)

So what does the loop do ?

It continuously looks into the Call Stack for some code to execute. As soon as the call stack is empty, it then looks into the Queue for some tasks to dispatch into the Call Stack and execute. So, its main role is **to connect the Call Stack with the Queue**.

## 2 types of queues

So, why is there 2 types of queues to handle asynchronous tasks ? This is a matter of **priority**.

The Job Queue (also called Micro tasks Queue) handles all high priority async operations. A few examples are:

```
Promises, processes.nextTick, queueMicrotask, etc.
```

The Task Queue (also called Macro task Queue) handles lower priority async operations. A few examples are:

```
setTimeout, setInterval, setImmediate, etc.
```

Whenever the Call Stack is empty, the Event Loop comes first to look into the Job Queue for the next task to handle. As long as there are tasks in that queue, it will keep picking from it.

This can lead to some unwanted infinite loop, because a micro task can return another micro task, which can return another task, which can ... You see what I'm getting at, so just be careful.

Once the Job Queue is empty, it’s the Task Queue turn. The Event Loop takes the top task of that queue and dispatches it to the Call Stack for execution. Once the iteration is done, it starts over, looking first to the Call Stack, then to the Job Queue for any other available tasks, before coming back to the Task Queue.

To summarize:

-   A loop iteration will first execute all the tasks from the Job Queue, before looking to execute one task from the Task Queue and finishing its iteration. The tasks stacked in the Job queue will be handled multiple times per loop iteration, while only one task of the Task queue will be handle during an iteration.
-   As long as there are tasks inside the Job Queue, the loop will continue processing those operations, before executing any task of the Task Queue.

```
console.log('1st message');

setTimeout(() => {
   console.log('2nd message: setTimeout');
}, 0);

const promise = new Promise((resolve, reject) => {
   resolve();
});
promise.then(resolve => {
   console.log('3rd message: promise');
})

console.log('4th message');
```

If you run the code above, the printed result will be:

```
"1st message"
"4th message"
"3rd message: promise"
"2nd message: setTimeout"
```

The `console.log` functions are immediately present in the Call Stack and are the first to be executed. Then, as the Job Queue takes priority, it is the 2 `Promise` functions that are executed. Finally, because the Job Queue is empty, it is the `setTimeout` function turn to be executed.

## Conclusion

Most of the time, there is no need to handle any particular Micro tasks. But it can help when trying to improve performances: for example, if we are writing a very heavy computation function, it might be interesting to chunk it in different pieces and handle it asynchronously, to prevent unresponsive browsers.

But we also need to be aware of the issues that can arise:

-   We might cause an infinite loop
-   Since a Micro task can queue other Micro tasks, it can take a very long time until the loop processes the next Macro task, resulting in a blocked UI for example.

## Some sources

-   [Using microtasks in JavaScript with queueMicrotask()](https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide#when_to_use_microtasks)
-   [In depth: Microtasks and the JavaScript runtime environment](https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide/In_depth)
-   [JavaScript Visualized: Event Loop](https://dev.to/lydiahallie/javascript-visualized-event-loop-3dif)
-   [Task Queue and Job Queue - Deep dive into Javascript Event Loop Model](https://blog.greenroots.info/task-queue-and-job-queue-deep-dive-into-javascript-event-loop-model)
-   [Javascript Event Loop - Why so Serious!](https://blog.greenroots.info/javascript-event-loop-why-so-serious#cjugsvvfa004mmts1hi8rfyf4)

## More articles

Author(s)

[

![Anthony](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2024/11/anthony-1-300x300.png)



](/en/agence/anthony-salamone)

[Anthony](/en/agence/anthony-salamone)28 September 2026

### [Smashing Conference Freiburg 2026](/en/blog/smashing-conference-freiburg-2026)

Back in Freiburg for Smashing Conference 2026: AI and design systems, deeply technical CSS and HTML, and more human talks about teams and mindset. Here's what we took away.

Back in Freiburg for Smashing Conference 2026: AI and design systems, deeply technical CSS and HTML, and more human talks about teams and mindset. Here's what we took away.

[

![Smashing Conference Freiburg 2026 poster](https://images.statique.ch/t/w_750,h_500,c_fill,f_jpeg,q_70/assets/blog/smashing-2026.png)



](/en/blog/smashing-conference-freiburg-2026)

Author(s)

[

![Akira](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2025/03/akira-small-300x300.jpg)



](/en/agence/akira-kamata)

[Akira](/en/agence/akira-kamata)22 July 2026

### [Making of : “La nuit des Losers”](/en/blog/making-of-la-nuit-des-losers)

lanuitdeslosers.ch, built fast: paper wireframes, a bold hand-drawn identity, then Claude Code to prototype and ship. A mouse-tracking hero animation, a bowling game made of sponsor logos, and a few bugs we’re keeping.

lanuitdeslosers.ch, built fast: paper wireframes, a bold hand-drawn identity, then Claude Code to prototype and ship. A mouse-tracking hero animation, a bowling game made of sponsor logos, and a few bugs we’re keeping.

[

![Making of : “La nuit des Losers”](https://images.statique.ch/t/w_750,h_500,c_fill,f_jpeg,q_70/wp/app/uploads/2026/05/La-nuit-des-Losers-Teaser-1-768x576.png)



](/en/blog/making-of-la-nuit-des-losers)

Author(s)

[

![Pierre](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2020/12/Pierre-300x300.jpg)



](/en/agence/pierre)

[Pierre](/en/agence/pierre)7 July 2026

### [Dark mode: necessity or trend](/en/blog/dark-mode-necessity-or-trend)

Nous faisons le tri entre idées reçues, réalités techniques et bonnes pratiques de design.

Nous faisons le tri entre idées reçues, réalités techniques et bonnes pratiques de design.

[

![Dark mode: necessity or trend](https://images.statique.ch/t/w_750,h_500,c_fill,f_jpeg,q_70/wp/app/uploads/2026/07/1-768x384.png)



](/en/blog/dark-mode-necessity-or-trend)
