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.

Something similar happens in code execution. For example, in the code written with JavaScript language, if necessary, a code can be executed without having to wait for all the previous codes to be executed and that’s it. But not all languages ​​have this capability. In some languages, everything must be in order.

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).

What is Async?

In order to explain about the Async function in JavaScript or Asynchrony in programming, I must first talk very well about synchronous and asynchronous programming. A programmer may need to write several thousand lines of code to write the codes of one page of a website. Each of these codes has specific but different functions and features. Some are related to requests from the server and some are executed by the browser.

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

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.

Asynchronous Programming

Asynchronous programming or asynchronous operations (Asynchronous Operations) or Nonblocking Programming is the opposite of the first. It means that several functions and codes can be executed together and do not disrupt the main flow of the written codes. Here, there is no more order or order. Code sets are executed in parallel. The most important application of this type of programming is when a request is made to the database and a result must be obtained from it. The loading of the rest of the page and the execution of other codes will not stop until that result is reached and the work is done. The only programming language that is asynchronous and runs asynchronously in the browser is JavaScript.

JavaScript and Asynchronous programming

JavaScript (JS) is a powerful and powerful language that has many libraries. It also has powerful frameworks. One of the most famous server-side frameworks of this language, which also allows asynchronous programming, is the Node framework. Asynchrony in JavaScript is because the programmer can control the execution order of functions. That is, the functions based on which they are written may not be read. JavaScript gives the programmer two important possibilities to control the execution time of functions.

The first possibility is Callbacks in JavaScript. With this possibility, the programmer can ask that function to call the execution of another function (that is, to start the execution of the second one) when a function is executed. The second possibility is known as Async in JS or Async/Await, which is implemented with the help of JS Promises.

Async/Await in JavaScript

In JavaScript, there is an object called Promise, which can contain two categories of code. Codes that must be executed and produce a result (Producing code) and codes that need that result and wait for that result, which is either successful or an error, remain (Consuming code).

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.


Leave a Reply

Your email address will not be published. Required fields are marked *