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

Chapter 2. Node.js Essential Patterns

Embracing the asynchronous nature of Node.js is not trivial at all, especially if coming from a language such as PHP where it is not usual to deal with asynchronous code.

In synchronous programming, we are used to the concept of imagining code as a series of consecutive computing steps defined to solve a specific problem. Every operation is blocking, which means that only when an operation is completed is it possible to execute the next one. This approach makes the code easy to understand and debug.

Instead, in asynchronous programming, some operations, such as reading a file or performing a network request, can be executed as an operation in the background. When an asynchronous operation is invoked, the next one is executed immediately, even if the previous operation has not finished yet. The operations pending in the background can complete at any time, and the whole application should be programmed to react in the proper way when an asynchronous call finishes.

While this non-blocking approach could almost always guarantee superior performance compared to an always-blocking scenario, it provides a paradigm that could be hard to reason about and that can get really cumbersome when dealing with more advanced applications that require complex control flows.

Node.js offers a series of tools and design patterns to deal optimally with asynchronous code. It's important to learn how to use them to gain confidence and write applications that are both performant and easy to understand and debug.

In this chapter, we will see two of the most important asynchronous patterns: callback and event emitter.

主站蜘蛛池模板: 桂阳县| 黔西县| 东乌珠穆沁旗| 枣阳市| 崇左市| 蓝山县| 镇平县| 泗水县| 天气| 读书| 潮州市| 临邑县| 库伦旗| 亚东县| 德昌县| 内黄县| 油尖旺区| 班玛县| 陆川县| 开原市| 汉川市| 古浪县| 偃师市| 连平县| 同仁县| 平原县| 绥德县| 浮梁县| 八宿县| 新巴尔虎右旗| 咸阳市| 平阴县| 日土县| 定日县| 盈江县| 阳高县| 达拉特旗| 安阳县| 交口县| 滕州市| 广东省|