Asynchronous Programming Vs. Synchronous Programming.
The True essence of programming lies in asynchronous functionality.
What is Programming? ๐ค
Programming refers to the process of designing or building an executable computer program to achieve a particular task. Usually, the process is simple, as of how we see it.
We write some lines of code, which is called the source code. Source codes are human-readable equivalent to executable code.
A specialized software, usually called a compiler, then takes the source code and converts it into machine language instructions, which can be directly understood by the operating system.
The operating system, then directly runs the executable code, and gives us a specific output.
This is the simplest breakdown of what happens under the hood.
Synchronous Vs. Asynchronous Operations.
Before going into the difference of their functionalities, let us get started with the terms first.
Synchronous Operations: refers to the occurrence of events dependant upon the main flow. It requires, one event must wait for another event to complete before it can begin.
Asynchronous Operations : refers to the occurrence of events independent of the main program. It means the events can be moved up and down the stack before a task is finished.
Confusing?๐คฏ Well, it is, not gonna lie. Therefore, I have come up with an analogy, for you all to understand better.๐
Analogy for Synchronous Operations.
Imagine you are at your college, sitting in a class. Suddenly, the teacher calls you to the board to write your name, and he also asks you to bring a book from the adjacent section, and write the name of the book on the board as well.
The immediate thought to achieve these two tasks in a reasonably convenient way would be, you get up, go out of the class, walk to the adjacent section, take the book, come back to your own class and write your name and the name of the book on the board, just like you were asked to do. Well done. Great job. This is an example of a Synchronous operation. In the essence of the work, you could also write your name on the board and then go to your adjacent section to get the book, come back to your class, and write the name of the book. Both instances are examples of Synchronous Operations.
Analogy for Asynchronous Operations.
Take the same example and the same task. There is also another way, you could have done the task, without wasting much time or being dependant upon the rest of the tasks. How? Simple. You ask one of your friends, to go to the adjacent section, get the book to you. And in the meantime, you do the simple task first, that is - walk up to the board and write your name. Just when you finish writing your name on the board, your friend comes into the class with the book and hands it over to you, and you write the name of the book, on the board, just like the teacher asked. This is an example of Asynchronous Operation. the events/tasks were carried out independently of each other, in contrast to the previous example, and saved a lot of time and energy.๐
Hope this analogy, helped you with the understanding of the two processes.
Let us take a look at their differences.
Asynchronous Programming.
Dependant on other tasks of the work unit.
Tasks are carried out one at a time.
More likely, used in designing programs that don't involve any independent process/tasks
Execution of any task is put on hold until the previous task is finished.
Deals with one request at a time.
Usually easy to read codes, but lesser efficient in the case of programs that involve independent processes.
Synchronous Programming.
Events carried out independent of the main workflow.
Events can be moved up and down the call stack, based on complexities, and the time for execution, irrespective of the completion of other tasks in the work unit.
Most likely to be used in designing programs, where independent processes are involved.
Deals with multiple requests at a time, hence completing more tasks carried out in a shorter period of time.
Callback functions are used for asynchronous purposes.
Increases code complexity, and increases efficiency too. However, asynchronous functions/operations are not much fruitful in the case of programs that do not involve any independent processes.
To wrap up, take a look at this picture, below :
Picture taken from : Google Images
Conclusion
Hope you are clear with the concepts and this blog was informative. If you found this helpful, please leave a reaction. ๐
I would like to end this topic with one of my analogies, trying to describe the essence of asynchronous functionalities...
CLI is to computer systems, is what asynchronous functions is to programming