Asynchronous programming
Asynchronous programming sounds mysterious, but the underlying concept is simple. For example, in traditional, synchronous programming, a program will initiate a request for a database record and then sit there and wait until the request is fulfilled. In asynchronous programming, however, the program will initiate a request to the database, specifying what should be done with the request’s result. The program will make a note of what is to be done then move on to the next task without waiting for the result to be returned. The specified result-handling logic is triggered only when the database request result returns. Asynchronous programs, in short, spend less time waiting around. In the context of programming, unnecessary waiting is referred to as “blocking.” Asynchronous logic and applications are referred to as “non-blocking.”
Asynchronous development is a bit like cooking a meal. If you were preparing a pasta dish, you wouldn’t wait until the water boiled to chop your vegetables and start frying them. You’d simply put water on the stove, turn on the heat, make a mental note of what needs to be done once the water boils, then immediately move on to chopping the vegetables. By the time the water is boiling, as shown in figure 1, you’d likely have the vegetables chopped and could fry them up as the pasta cooks.
Because the sequence of logic is more variable in asynchronous development, programming requires a different mindset. There are a number of techniques and third-party add-ons that can be employed to manage asynchronous execution at a higher level.
Asynchronous programming sounds mysterious, but the underlying concept is simple. For example, in traditional, synchronous programming, a program will initiate a request for a database record and then sit there and wait until the request is fulfilled. In asynchronous programming, however, the program will initiate a request to the database, specifying what should be done with the request’s result. The program will make a note of what is to be done then move on to the next task without waiting for the result to be returned. The specified result-handling logic is triggered only when the database request result returns. Asynchronous programs, in short, spend less time waiting around. In the context of programming, unnecessary waiting is referred to as “blocking.” Asynchronous logic and applications are referred to as “non-blocking.”
Asynchronous development is a bit like cooking a meal. If you were preparing a pasta dish, you wouldn’t wait until the water boiled to chop your vegetables and start frying them. You’d simply put water on the stove, turn on the heat, make a mental note of what needs to be done once the water boils, then immediately move on to chopping the vegetables. By the time the water is boiling, as shown in figure 1, you’d likely have the vegetables chopped and could fry them up as the pasta cooks.
Because the sequence of logic is more variable in asynchronous development, programming requires a different mindset. There are a number of techniques and third-party add-ons that can be employed to manage asynchronous execution at a higher level.
Until Node, web frameworks were largely based on languages without built-in support for asynchronous
programming: Perl, PHP, Java, Python, and Ruby. While third-party libraries for these languages support event-
driven programming, they also require additional knowledge, sometimes with considerable learning curves. If
you’ve explored alternatives to Node, you’ll likely come to appreciate Node’s elegant approach.
No comments:
Post a Comment