Sitemap

JAVASCRIPT — SINGLE THREADED AND ASYNCHRONOUS

6 min readOct 16, 2021

I have come to understand that I comprehend more by writing, Hence the need for this article.

For so long, I have been having issues Comprehending the concept of JavaScript being “Single threaded and Asynchronous” at the same time. I know my understanding of Single threading means it executes tasks one after the other line by line, and my understanding of Asynchronous means it can handle multiple tasks at once. and there goes my confusion. As far as I’m concerned, this two statements contradict each other. Am I wrong?, or is there something that I’m missing. after searching for long for a way out, I am proud to announce that I have found a breakthrough.

Press enter or click to view image in full size
Figure 1.0 : How I feel right now

So, I came across this wonderful video by Andrea that explains it all, completely clears my confusion. Seriously guys, it’s wonderful. Andrea mentioned that JavaScript being Single-threaded means it has only one Call Stack. I’m sure we know what Call Stack is right?. if you don’t , that’s fine.

Call Stack is one of the 2 key things that makes up a JavaScript V8 engine. The second is Memory Heap, and both are the tools used by the browser’s JavaScript Engine(the v8 engine). The Memory Heap is a space in memory where a variable is allocated or stored, while the Call Stack is responsible for keeping the flow of execution of programs, it tells you what point you are in the execution of tasks. Without it, JavaScript wouldn’t know what to call or when.

Figure 1.1 : The V8 engine
Figure 1.2 : Overview of the chrome’s V8 engine

So back to the matter, Andrea explained that some programming languages may have multiple Call Stack (i.e. are multithreaded), JavaScript however has just one Call Stack (which makes it single threaded). Tasks / Programs to be executed are pushed unto the top of the stack , and when it’s done executing, they are popped out of the stack. The Call Stack executes tasks one after the other using the LIFO (Last In First Out) Data Structure. for example :

//Call Stack

  1. console.log(‘1’);
  2. console.log(‘2’);
  3. console.log(‘3’);
  4. console.log(‘4’);

To execute the program above, line 1 is pushed unto the Call Stack, and it’s executed (the browser prints ‘1’ on the console), then it’s popped off (removed from) the stack. At this point the Stack becomes empty, line 2 is pushed unto the stack and executed (console prints ‘2’), then its popped off. This continues until all lines have been executed (up till line 4), and the Stack becomes empty again. So the flow of execution becomes :

line 1 => line 2 => line 3 => line 4

The type of execution above is Synchronous and Blocking. Synchronous as it is executed one after the other line by line, Blocking as line 1 must finish executing before line 2 can begin. This Technique can be slow when you have tons of lines of code to execute, or in image processing. To make it faster and more efficient, the concept of Non-blocking (Asynchronous) execution was introduced.

Pheeeeew, that was a lot of information, I hope you are following. Now let’s go through the concept of Asynchronous Coding.

Get Owuri Hanifat’s stories in your inbox

Join Medium for free to get updates from this writer.

Remember how I mentioned that Synchronous execution of code means the code is executed line by line?, Asynchronous JavaScript gives you control over how you want to execute your program (the flow of execution), what line is executed first, what line comes next?, you have the power to decide. And this power is called setTimeOut. Consider the code below :

  1. console.log(‘1’);
  2. setTimeOut( function task() => {
    console.log(‘2’);
    }, 2000 )
  3. console.log(‘3’);

The setTimeOut method gives you the power to specify the flow of execution of your program. It takes in 2 parameters. A call-back function that contains the code to be run, and the delay time (i.e. the number of seconds the web API should delay before pushing the function unto the Call Stack for execution)

What is a Web API?. In order for the JavaScript engine (with Call Stack and Memory Heap) to work effectively, it needs to be supported by the JavaScript run-time environment. The JavaScript run-time environment is included as part of the browser. Figure 1.3 below shows the run-time environment used by JavaScript, the Web API’s sits on top of the JS (v8) engine, as well as Callback Queue and Event Loop. The setTimeOut method is part of the Web API, the browser gives us to use so we can run asynchronous programs.

Press enter or click to view image in full size
Figure 1.3 : JavaScript run time environment
Press enter or click to view image in full size
Figure 1.4 : chrome’s JavaScript run time environment

So, back to our lines of code, what do you think will happen?, what if I told you that line 3 will be executed before line 2, strange right?. Here’s what happens :

line 1 is pushed unto the Call Stack for execution, when it’s done(prints ‘1’ on the console), it gets popped out of the Stack. then line 2 is pushed unto the Stack, as the Call Stack comes across the setTimeOut method, it understands that it is a Web API method, and therefore pops line 2 off and sends it to the Web API to be processed, for the set delay time (remember that our set delay time is 2000 milliseconds i.e. 2 seconds). After 2 seconds (when the Web API is done processing the function), line 2 is pushed back unto the Call Stack for execution. Meanwhile, during this whole process of pushing and popping, other activities regarding the Call Stack continues, and as such line 3 gets processed and executed before line 2. This is the “Non-blocking concept of Asynchronous JavaScript”. It is non-blocking in the sense that line 2 being sent to the Web API for processing doesn’t stop line 3 from being executed by the Call Stack. and therefore the flow becomes:

line 1 => line 3 => line 2

To reiterate, Consider the figure below :

Press enter or click to view image in full size
Figure 1.5 : The setTimeOut pushed unto the call Stack is sent to the Web API for processing
Figure 1.6 : the Call Stack now empty, can execute other tasks that don’t require Web API Processing
Press enter or click to view image in full size
Figure 1.7 : After 2 seconds (as specified), the call back function (line 2) is pushed unto the Call Stack and executed

I hope with these few points of mine, I have been able to convince you that JavaScript isn’t so hard. Seriously guys, I hope this clears your confusion. If not, check out the following links to gain more insight. Don’t forget to give a thumbs up if this article was helpful. and before I forget, you can connect with me on LinkedIn to stay abreast with the amazing things happening in my life. Till we meet again, bye for now.

1 . How to understand Single Threaded JavaScript and how it works- By Little Void

2. JavaScript setTimeOut

Owuri Hanifat
Owuri Hanifat

Written by Owuri Hanifat

Introvert, Front-end Developer, Aspiring software engineer, Tech lover, Machine learning enthusiast, good team player. open to Tech jobs