Node module mechanism and asynchronous processing _ node. js

Source: Internet
Author: User
This article mainly introduces the node module mechanism and detailed information about asynchronous processing. For more information, see 1. Module Mechanism

The purpose of the commonJS module mechanism is to build an ecosystem of js in terms of web servers, desktop programs, and browsers. Node js is an implementation of this specification. It uses requird to introduce other files. Similarly, npm follows the package specification defined by commonJS, thus forming a complete ecosystem.

Define and export modules

For example, there is a file named circle. js.

exports.getName = function(name) {  return name}

Module Loading

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

Module loading policy

The node module can be divided into two types:

Native modules and custom modules. The so-called native modules are node-defined modules, such as HTTP and fs modules. These modules are the fastest to load.
Another type is custom modules, including package modules and custom files. Both native and custom modules will be cached by node after the first loading, so there will be no overhead for the second request.

Native module loading:

After resolving the file name, The require () method first searches in the module cache, and then searches in the native module of node.
Load from File
Module. path has a paths attribute when creating the module object for each loaded file module. The path points to the path of the introduced module.
When the absolute path module is requested, it does not traverse node_module for the fastest loading speed.

Asynchronous programming

High-resolution Functions
A high-resolution function is a function that uses a function as a parameter or as a return value.

function foo(X) {  return function() {    return x  }}

The biggest feature of NODE is the opportunity event-driven non-blocking IO.

Asynchronous programming Solution

1. Event publishing/subscription Mode

The event listener mode is widely used in asynchronous programming and is a time-based callback function, also known as the publish/subscribe mode.

// Subscribe to emitter. on ("event1", function (message) {console. log (message)}) // publish the emitter. emit ("event1", "I am message ")

2. Promise Mode

The promise object has three states: incomplete state, complete state, and failure state.
The promise object must have the then method and has the following requirements for the then method:

1. Accept the callback method of the completion state and the error state.
2. Only function is accepted, and other objects will be ignored.
3. Continue to return the promise object for chained calling

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.