The events module of the node module

Source: Internet
Author: User
Tags emit

The events module provides only one object: events. Eventemitter.

"The core of Eventemitter is the encapsulation of event triggering and event listener functionality. 】

Each event of the Eventemitter consists of an event name and several parameters, which is a string that usually expresses a certain semantics. For each event, Eventemitter supports a number of event listeners. When an event is triggered, the event listener registered to the event is called sequentially, and the event arguments are passed as a callback function parameter.

Example:

var events = require (' Events '); var New events. Eventemitter (); Emitter.on (function(arg1, arg2) {console.log (' Listener 1 ', Arg1, Arg2)}); Emitter.on (  function(arg1, arg2) {console.log (' Listener 2 ', Arg1, Arg2)}); Emitter.emit (' an event name ', ' Hello ', world) ;

The result of the operation is:

Listener 1 Hello World listener 2 Hello World

In the example above, emitter registers a single event listener for an event and then triggers an event. Then two event listeners are called successively.

Eventemitter Common APIs:

Eventemitter.on (event, listener)  

// registers a listener for the specified event, accepting a string event and a callback function listener.

// triggers the event events, passing several optional parameters to the event listener's parameter table.
// registers a single listener for the specified event, that is, the listener fires at most once, and then immediately unlocks the listener
// removes a listener for the specified event, listener must be a listener that the event has already registered. 
// Removes all listeners for all events, and removes all listeners for the specified event if event is specified. 

For more APIs, refer to: http://nodejs.cn/api/events.html

"Error Event"

Eventemitter defines a special event error, which contains the semantics of "error", and we usually emit an error event when we encounter an exception. When error is triggered, eventemitter specifies that if there is no response listener, node. JS will treat it as an exception, exit the program and print the call stack. We typically set up listeners for the object that will emit the error event, to avoid the entire program crashing after encountering errors.

Example:

var events = require (' Events '); var New events. Eventemitter (); Emitter.emit (' error ');

The Run Times error is as follows:

error:unhandled "Error" event at    eventemitter.emit (events.js:175:14)

"In most cases we do not go directly to the eventemitter, but inherit it from the object. 】

Including FS, NET, HTTP, as long as the core modules that support event response are Eventemitter subclasses.

The events module of the node module

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.