官术网_书友最值得收藏!

Non-blocking asynchronous execution

One of the most powerful features of Node.js is that it is both event-driven and asynchronous. So, how does an asynchronous model work? Imagine you have a block of code and at some nth line you have an operation that is time consuming. What happens to the lines that follow the nth line while this code gets executed? In normal synchronous programming models, the lines that follow the nth line will have to wait until the operation at that line completes. An asynchronous model handles this case differently.

Let us visualize this scenario with the help of the following code and diagram:

In the preceding case, the setTimeout() method is provided by JavaScript (Node.js) API. Hence, this method is recognized as synchronous and is executed in a different execution context. According to functionality to setTimeout() , it executes the callback function after a specified duration, in our case after three seconds.

Further, the current execution is never blocked for a process to complete. When Node.js API determines that the completion of an event has been fired, it will execute your callback function at that moment.

In a typical synchronous programming language, executing the preceding code will yield the following output:

If you are still interested in learning more about asynchronous models and the callback concept in JavaScript, Mozilla Developer Network ( MDN) has many articles that explain these concepts in detail.
主站蜘蛛池模板: 溧水县| 弥勒县| 武山县| 洪江市| 易门县| 东至县| 香河县| 本溪市| 建水县| 明光市| 舒兰市| 桂林市| 邯郸县| 南宁市| 收藏| 青海省| 重庆市| 安阳市| 崇文区| 郴州市| 陵川县| 宁国市| 望城县| 咸宁市| 南木林县| 滕州市| 区。| 潞城市| 邵阳市| 大庆市| 会昌县| 武隆县| 安新县| 扬州市| 七台河市| 新建县| 阜南县| 海盐县| 禄丰县| 谢通门县| 贞丰县|