Concurrency In JavaScript
Javascript is a single threaded language. This means it has one call stack and one memory heap. As expected, it executes code in order and must finish executing a piece code before moving onto the next. So how does concurrency work in javascript without blocking other functions from executing? before we answer to this question let’s make sure you know the difference between concurrency and parallelism, a related but quite distinct concept.
- Concurrency refers to things happening in some unspecified order. Multitasking — executing multiple programs by interleaving instructions via time slicing — is a good way to think about this sense of concurrency.
- Parallelism (or “true” parallelism) refers to things happening at literally the same time. This requires hardware support (coprocessors, multi-core processors, networked machines, etc.). All parallelism is concurrent, but not all concurrency is parallel.
So, how do we implement concurrency tasks? Well, there are typically 2 ways, to tackle this:
- blocking (synchronous). This pattern heavily relies on multithreading as it needs to continue to serve user requests independently if one of the threads in the list is blocked or not. The blocking style basically stops the execution of a thread and waits for the task to be completed before moving on with that thread. PHP, Ruby and most server-side technologies implement this approach. Since everything is done sequentially, code written following this pattern is readable and also easier to be implemented and maintained.
- non-blocking (event loop). This style relies on a single thread — everything runs in that one thread — which means we cannot block the thread, otherwise the program would stale until the thread is unblocked. This pattern is especially good because we get very high concurrency with low memory consumption. It is also very good for user experiences, as there is always a continuous flow in the application. Thus, that’s why it’s the pattern style adopted by browsers and of course, by JavaScript.
Let’s explain non-blocking (JavaScript way) by using an example, imagine you need to show user details in their profile which includes DOB, location, age but you saved these data in different branches of your database, so you need to send multiple HTTP requests to fetch all the data you need (this is not how you usually save data in database but trying to make a point).
Let’s see why we are only getting location and age but not dob.
Although they run sequentially but that doesn't mean we get the data before we run the next function. In order to fix this, we need to understand await/async and promise.all
The word async before a function means a function always returns a promise. Other values are wrapped in a resolved promise automatically.
Get Aliajam’s stories in your inbox
Join Medium for free to get updates from this writer.
The keyword await makes JavaScript wait until that promise settles and returns its result.
When we need to wait for multiple promises, we can wrap them in Promise.all and then await.





