Home > Web Front-end > JS Tutorial > Detailed explanation of node module mechanism and asynchronous processing_node.js

Detailed explanation of node module mechanism and asynchronous processing_node.js

WBOY
Release: 2016-05-16 15:10:43
Original
2076 people have browsed it

1. Module mechanism

The purpose of the commonJS module mechanism is to build js to form an ecosystem in web servers, desktop programs, browsers, etc. Node js is an implementation of this specification, using require to introduce other files. Similarly, npm also follows the package specification defined by commonJS, thus forming a complete ecosystem.

Module definition and export

For example, there is the following file named circle.js

exports.getName = function(name) {
  return name
}
Copy after login

Module loading

var circle = require('/circle.js')
console.log(circle.getName('WPY'))
Copy after login

Module loading strategy

Node modules can be mainly divided into two categories:

Native modules and custom modules. The so-called native modules are modules defined by node itself, such as HTTP and fs modules. These modules have the fastest loading speed.
There is also a category of custom modules, including package modules and files we define ourselves. Whether it is a native module or a custom module, it will be cached by node after being loaded for the first time, so there will be no overhead on the second request.

Native module loading:

After parsing the file name, the require() method will first search in the module cache, and then search in the native module of node
Load from file
module.path For each loaded file module, there will be a paths attribute when the module object is created. Its path points to the path of the imported module.
When requesting an absolute path module, node_module will not be traversed for the fastest loading speed.

Asynchronous Programming

High intermediate function
A high-level function is a function that takes a function as a parameter or as a return value.

function foo(X) {
  return function() {
    return x
  }
}
Copy after login

The biggest feature brought by NODE is the opportunistic event-driven non-blocking IO.

Asynchronous programming solutions

1. Event publishing/subscription model

The event listener mode is widely used in asynchronous programming. It is a timed callback function, also known as the publish/subscribe mode.

//订阅

emitter.on("event1", function(message){
  console.log(message)
})
//发布

emitter.emit("event1", "I am message")

Copy after login

2.Promise mode

The promise object has three states, unfinished state, completed state and failed state.
The promise object must have a then method. The then method has the following requirements

1. Accept callback methods for completion status and error status.
2. Only functions are accepted, other objects will be ignored
3. Continue to return the promise object to implement chain calls

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template