- 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:

- PHP 從入門到項目實踐(超值版)
- JIRA 7 Administration Cookbook(Second Edition)
- 人臉識別原理及算法:動態(tài)人臉識別系統(tǒng)研究
- Java 11 Cookbook
- Reactive Android Programming
- 組態(tài)軟件技術與應用
- Nginx Lua開發(fā)實戰(zhàn)
- 基于ARM Cortex-M4F內核的MSP432 MCU開發(fā)實踐
- 0 bug:C/C++商用工程之道
- 響應式Web設計:HTML5和CSS3實戰(zhàn)(第2版)
- Node.js 12實戰(zhàn)
- Java Web應用開發(fā)給力起飛
- 玩轉.NET Micro Framework移植:基于STM32F10x處理器
- 零基礎學C++(升級版)
- Spring Data JPA從入門到精通