What is Asynchronous Programming? Why is JavaScript a language for asynchronous programming?

What is Asynchronous Programming? Why is JavaScript a language for asynchronous programming?

Daily short news for you
  • Privacy Guides is a non-profit project aimed at providing users with insights into privacy rights, while also recommending best practices or tools to help reclaim privacy in the world of the Internet.

    There are many great articles here, and I will take the example of three concepts that are often confused or misrepresented: Privacy, Security, and Anonymity. While many people who oppose privacy argue that a person does not need privacy if they have 'nothing to hide.' 'This is a dangerous misconception, as it creates the impression that those who demand privacy must be deviant, criminal, or wrongdoers.' - Why Privacy Matters.

    » Read more
  • There is a wonderful place to learn, or if you're stuck in the thought that there's nothing left to learn, then the comments over at Hacker News are just for you.

    Y Combinator - the company behind Hacker News focuses on venture capital investments for startups in Silicon Valley, so it’s no surprise that there are many brilliant minds commenting here. But their casual discussions provide us with keywords that can open up many new insights.

    Don't believe it? Just scroll a bit, click on a post that matches your interests, check out the comments, and don’t forget to grab a cup of coffee next to you ☕️

    » Read more
  • Just got played by my buddy Turso. The server suddenly crashed, and checking the logs revealed a lot of errors:

    Operation was blocked LibsqlError: PROXY_ERROR: error executing a request on the primary

    Suspicious, I went to the Turso admin panel and saw the statistics showing that I had executed over 500 million write commands!? At that moment, I was like, "What the heck? Am I being DDoSed? But there's no way I could have written 500 million."

    Turso offers users free monthly limits of 1 billion read requests and 25 million write requests, yet I had written over 500 million. Does that seem unreasonable to everyone? 😆. But the server was down, and should I really spend money to get it back online? Roughly calculating, 500M would cost about $500.

    After that, I went to the Discord channel seeking help, and very quickly someone came in to assist me, and just a few minutes later they informed me that the error was on their side and had restored the service for me. Truly, in the midst of misfortune, there’s good fortune; what I love most about this service is the quick support like this 🙏

    » Read more

The Issue

JavaScript is a language for asynchronous programming - that's something everyone knows. Asynchronous operations are most evident in asynchronous functions, which do not immediately return a result but instead return it at some point in time.

If you come from a background of PHP, Go, or even Java... you may have not heard or be confused about what asynchronous programming is. You may also wonder why JavaScript is a language for asynchronous programming and whether it brings any benefits or limitations to the language.

In today's article, I will analyze the reasons why JavaScript is a language for asynchronous programming. In other words, why it "should" be a language for asynchronous programming.

What is Asynchronous Programming?

Asynchronous programming is a programming method that allows tasks to be performed independently and in parallel without having to wait for other tasks to complete. Instead of waiting for a task to complete and return a result before starting the next task, asynchronous programming allows tasks to be run concurrently, which improves performance and response time.

In JavaScript, asynchronous programming is evident in asynchronous functions, which return results through callback functions, promises, async/await, event listeners, and more. For example, asynchronous API calls, user events (such as mouse movements, clicks, and focus), and actions performed in Node.js (such as database queries and file operations) are all asynchronous actions.

Why is JavaScript Asynchronous?

JavaScript was originally designed to run on browsers through web applications. Because of the nature of browsers, JavaScript had to be designed in an asynchronous manner to improve performance.

Let's imagine a hypothetical situation where JavaScript runs synchronously. While a web page is being loaded and processed for user display, an Ajax call is made to a server to retrieve data. In this scenario, the rendering process would be "paused" waiting for the result. What if the network is slow or the server responds slowly? The web page would not function properly because it has to wait for functions that "take a long time".

Fortunately, Ajax calls are asynchronous operations, so the rendering process can still proceed normally.

Web applications consist of a combination of user events. Users scroll, click buttons, type input... To handle these events, JavaScript needs to create event listeners to perform corresponding tasks when they occur. This cannot be achieved in synchronous programming. Event listeners allow programmers to create handling functions that are called when certain user actions occur.

Thanks to this, we now have many excellent asynchronous programming frameworks such as Node.js, Angular, and React, which help developers efficiently develop web applications and improve application response time.

In conclusion, JavaScript "should" be designed as an asynchronous language to meet the needs of modern web applications, improve performance, and be compatible with browser structures.

Pros and Cons

One of the greatest advantages of asynchronous programming is improved performance for applications. It speeds up the execution of tasks within an application by allowing them to be performed in parallel without waiting for other tasks to complete. Tasks can be performed without blocking the main application thread, preventing system hang-ups.

With asynchronous programming, large tasks can be divided into smaller parts and executed simultaneously. This improves performance and reduces execution time for large tasks.

However, asynchronous programming also brings certain challenges. Firstly, there is a learning curve and familiarizing oneself with the asynchronous style, which may take some time to understand both its concepts and style.

Handling errors and debugging can be more complex in asynchronous programming. Since tasks are executed simultaneously, if one task encounters an error, other tasks may continue running normally, making error handling more complicated. Additionally, if tasks overlap, debugging can be challenging.

Whether it is synchronous or asynchronous programming, resource management and sharing are necessary skills. With asynchronous programming, multiple tasks may access shared resources concurrently, so precautions and problem-solving skills are needed to avoid conflicts.

Conclusion

Asynchronous programming is a programming method that allows tasks to be performed independently and in parallel without having to wait for other tasks to complete. JavaScript is a language for asynchronous programming, and this gives it many advantages in the browser environment. Additionally, Node.js leverages the power of asynchronous programming to create high-performance servers. However, asynchronous programming also brings certain challenges such as the initial learning curve, error handling, and resource management.

Premium
Hello

Me & the desire to "play with words"

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (0)

Leave a comment...