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

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.
主站蜘蛛池模板: 恩施市| 宝山区| 林甸县| 新蔡县| 金湖县| 临西县| 秦安县| 三门峡市| 滦南县| 固镇县| 班玛县| 禹州市| 青龙| 南阳市| 岳普湖县| 临湘市| 奎屯市| 龙游县| 柳江县| 拉孜县| 大石桥市| 保康县| 台中市| 清水河县| 杭锦后旗| 德惠市| 贞丰县| 西林县| 新津县| 新竹县| 开阳县| 马边| 布尔津县| 阿坝县| 汉寿县| 南昌县| 体育| 太康县| 岢岚县| 普定县| 敖汉旗|