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

A primer on Node

As JavaScript developers who may or may not have experience working with Node.js, a brief introduction to Node.js and its core ideology will help get everyone up to speed. Node.js, or simply Node, is a run-time environment that executes JavaScript outside a browser. In simpler terms, and as it relates to web developers, Node is a platform that allows developers to write JavaScript applications that can also act as servers.

JavaScript became popular for being a language used to manipulate the DOM (Document Object Model) on web pages. It was a language typically used for client-side scripting. Node, which was built on Chrome's open source v8 JavaScript engine, made it possible to run JavaScript both on the browser and the server. This was highly accepted, as developers could now develop applications with the same language on servers and web browsers.

Node is very fast and is a great choice for building HTTP applications. It processes incoming requests in a loop, called the event loop, which allows the development of fast web servers in JavaScript. Its event-driven architecture allows asynchronous operations. This means that developers can create highly scalable applications capable of processing requests asynchronously without using threading.

Asynchronous programming in Node is one of the reasons the language is so widely adopted. If you are unfamiliar with asynchronous programming or its benefits and how it compares to synchronous programming, here is a good example of a program that needs to make a request to get data from two external sources:

  • In a synchronous program: The logical thing to do would be to make a request to the first external source, get a response, and then make another request to the second external source and merge the results. While this is a flow that is logical and easy to follow, it means that the wait time to service another request will be at least the sum of the wait times for each individual request. Since synchronous code leads to resource and event blocking, it does not lead an efficient solution and effectively slows down our application due to poor resource utilization.
  • In an asynchronous program: Both requests can be made in parallel. When each request is completed, it notifies the main program and the results can be combined after the request that took the longest is completed. In this case, the wait time is only the time it takes for the slower request to be completed. Also, neither of the requests cause resource/event blocking, which would allow our program to respond to more new requests while waiting for results for the initial task.

Managing asynchronous actions can get quite complicated, especially in programs where the flow of logic should be synchronous. Callback functions can be used to manage asynchronous operations. Callback functions are functions that are passed to another function (the main function) to be executed inside the main function. Here's a simple example of using a callback function with the setTimeout() function:

function logTimeUp() {
console.log(“Time up!”);
}

setTimeout(logTimeUp, 1000);

The setTimeout function in JavaScript waits a given number of milliseconds and then executes the callback function passed to it. In the previous code example, we define a callback function called logTimeUp that simply prints Time up! to stdout. We then pass this function as a parameter to the setTimeout function, which will execute the callback function after 1000 milliseconds (one second). This is a classic example of how callbacks work.

In modern JavaScript, asynchronous actions can be modeled using Promises, which can be managed and consumed in multiple ways. One of these ways is using the async… await syntax.

主站蜘蛛池模板: 莲花县| 六安市| 灵寿县| 富顺县| 皮山县| 万山特区| 犍为县| 乐山市| 高平市| 汨罗市| 内丘县| 汉寿县| 香港 | 襄垣县| 宜良县| 洮南市| 格尔木市| 竹溪县| 汨罗市| 遵义市| 正镶白旗| 汕尾市| 阜新市| 当阳市| 凭祥市| 青龙| 黄浦区| 改则县| 大化| 色达县| 固安县| 兴和县| 二连浩特市| 甘孜县| 县级市| 甘泉县| 大渡口区| 诸城市| 海林市| 贵溪市| 阜新市|