The world of programming and website design is an interesting world. A world where programming languages and their capabilities and functions gradually improved and became better. If you hear that programming may be synchronous or asynchronous (Synchronous Programming or Asynchronous Programming), what comes to your mind? What if you hear that a function can be asynchronous? Why should such things be important in programming? Finally, the programmer writes the codes and the browser executes them.
Let me give an example before explaining further. When we go to the bank, we have to queue. But the numbers (turns) are different based on what we want to do in the bank. That is, the turns are not in order. If I work as a finance minister, my number will be 5, and you, whose job is electronic banking, will be number 1. So you go ahead of me and do your work. I have to wait for the 4 people in front of me to finish their work and leave for my turn.
How does the browser read the codes? In what order does it have to start from the first line and come in order until the end? It means that it is not possible to not follow the order and execute one code earlier than the others? Basically, why should the order of code execution be important?
This discussion is not simple at all. In this content, I want to answer the questions that were raised and talk about the order of code execution (turn).
Table of Contents
What is Async?
It is important for the user and the developer that all the code is executed very quickly and the page loads completely. Maybe the user has a request from the site or wants to do something on the site. So maybe the execution of some codes requires receiving a result from the server and several trips back and forth between the server and the page. So, for this reason, the order of code execution is important. Many codes must be executed at the same time (synchronously) so that no problem occurs and the user does not wait for anything.
Synchronous programming or synchronous operations (Synchronous Operations) or Blocking Programming happens when the complete execution of codes (functions) prevents the execution of other codes. That is, the command to execute no other code is executed until a task is finished. If we go back to the example of the bank, you have to wait until the work of the 4 people in front of you is finished and then it is your turn. Those 4 people in front have blocked your way. Of course, synchronous programming has its uses. It’s most important application is in making Real-time Systems. Argus, Signal and Luster languages are special synchronous programming languages. Other languages like Java, Python, and C# are also synchronous, but there are ways to write asynchronous code with them.
When the word Async is written before a function, that function must execute a Promise and, when the result is determined, do something based on that result. But when the word Await is written before a function, that function must wait for the result of an object to be determined and then be executed. For example, the programmer can give time to two functions. That is, write the code in such a way that the second function is executed 5 seconds after the result of the Promise is determined.