Asynchronous JavaScript, behind the scenes
Maybe, the most important concept in JavaScript is Asynchronism. This capability enabled us to improve the user experience of our web applications. But, there are other related concepts (concurrency, parallelism, blocking and non-blocking behaviors) behind the scenes that we should understand first before going deeper with JavaScript’s asynchronous model.
As a frontend developer I firmly believe that you must master JavaScript (commonly known as Vanilla JavaScript) before starting to learn any of its frameworks like Vue, Angular or React. If you dominate JavaScript, it will be easier to learn any framework for sure.
With no doubts, Asynchronism is one or maybe the most important concept that a frontend developer must master before starting with any frontend/JavaScript application. Thanks to this ability, we can create web applications that perform complex tasks where we need to request any operation or resource and then wait for its result, without affecting the user experience (users hate to wait!). The most common case is when we need to retrieve data from the server.
But what asynchronism has to do with JavaScript ? well, the thing is that:
JavaScript has an asynchronous non-blocking model, with an event-loop process implemented over a single execution thread.
Asynchronous non… event… single… what? Let me break this down from the beginning.
Concurrency and Parallelism
Let’s start with a couple of concepts that refer to the way some tasks can be executed. They are often used indistinctly, but we can find differences if we look through a magnifying glass:
Concurrency is when two or more different tasks progress in the same lapse. Those tasks don’t have to be executed at the same instant and require one or more task runners (something or someone who performs the task). Yes, concurrency does not necessarily mean this should have two task runners.
The intertwined technique is an example of concurrency with a single task runner which breaks the tasks into smaller tasks and interleaves them so that each one will run for a short time. However, in the long term, the impression is that they are all progressing at the same time.
Parallelism is when two or more different tasks are executed at the same time and demands a minimum of two or more task runners. We can see parallelism as a particular case of concurrency.
Imagine a small restaurant where its unique chef (task runner) has orders (tasks) to prepare three meals. He decides to begin preparing the three meats, then he chops all vegetables for the three salads and finally He cooks the potatoes. The chef has concurrently worked on the three meals, or, the three meals were cooked in series, but in a concurrent way.
Imagine now a second scenario where the restaurant has two chefs to prepare two meals, and each chef works on the meat, the salad and the potato of each meal. We can then say that the two meals were prepared in parallel.
A is not concurrent nor parallel, is just sequential execution. C is concurrent with a single thread (intertwined). B and D are parallelism
CPU-bound operations vs I/O-bound operations
Commonly, the tasks that don’t need external resources are executed in the context of our application, in our browser (in frontend scope), in our PC, they are known as CPU-bound.
Get David Moreno’s stories in your inbox
Join Medium for free to get updates from this writer.
However, there are other tasks where we have to request things from an external source. For example: getting info from a file in the hard disk or a database or even from the internet. Those are tasks that are performed and completed outside of the context of our application, which has to wait for the result. They are known as I/O-bound (Input/Output).
In the last analogy, the chef is I/O-bound when he has to wait for a special ingredient supplied by an external provider that has to import it from another county, before the restaurant can offer a meal with that ingredient.
We then say that CPU-bound tasks are synchronous, that is, the next task cannot be executed until the current task is finished. However, it’s different for tasks that are out of the application’s control, where it’s uncertain when the external task runner is going to provide an answer, and the application must wait for it.
I/O: Blocking vs non-blocking
…and the application must wait for it. We conclude that I/O-bound tasks go through two phases: The waiting and the execution phases. It’s in the waiting phase where we talk about blocking or non-blocking.
In the first one, the task performer is blocked during the execution of one task, and it has to complete it before processing anything else. We can say this task performer is a synchronous single thread.
In the second one, the task runner simply “delegates” the (blocking) task to another external task runner, so that it is free and available to (synchronously) continue with the rest of the tasks. Once the external task runner is done, it sends a notification to let the first task runner know that the delegated task is complete and it can resume its process with the response as soon as it is available. We then refer to this task performer as an asynchronous single thread.
JavaScript Model
This programming language was originally intended to make the user interfaces more interactive with both the user and the server (I/O interactions), while the application is always fluid in the browser.
Note that the normal flow of code execution in JavaScript is synchronous, and it’s ok for CPU-bound tasks, but, If I/O-bound tasks were synchronous too, the application would be blocked each time it requests and waits for the server’s response. Users are not aware of the process that’s happening behind the scenes, they simply notice that their browser is frozen, causing confusion and leading to a terrible user experience.
Finally, that’s why JavaScript has an asynchronous non-blocking model in a single thread, that is implemented with an event loop. The Event Loop is a topic that is already explained by other writers in Medium.
And there we have it. I hope you have found this useful. Thank you for reading.
More content at PlainEnglish.io.
Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.








