- Web Development with MongoDB and Node(Third Edition)
- Bruno Joseph D'mello Mithun Satheesh Jason Krol
- 233字
- 2021-07-08 10:32:36
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:

- Hyper-V 2016 Best Practices
- 潮流:UI設計必修課
- 微服務與事件驅動架構
- PHP程序設計(慕課版)
- MySQL 8 DBA基礎教程
- C語言程序設計
- Python進階編程:編寫更高效、優雅的Python代碼
- Java虛擬機字節碼:從入門到實戰
- INSTANT Mercurial SCM Essentials How-to
- Mastering Apache Maven 3
- Learning OpenCV 3 Computer Vision with Python(Second Edition)
- C語言開發基礎教程(Dev-C++)(第2版)
- 詳解MATLAB圖形繪制技術
- Windows Phone 8 Game Development
- Getting Started with Web Components