Creator of node.js

Node.js EventEmitter - Error Handling

In Node.js, EventEmitter is use to “emit” an errors and also when your object emits lots of types of events.

There are multiple objects that inherit from EventEmitter and the “error” events emitted in Node objects looks like,
1.      Streams
2.      Servers
3.      Requests and Responses
4.      Child Processes etc.

//EventEmitter to “emit” errors.
var emitr = new (require('events').EventEmitter)();

//Calling asyncEmitter method.
var event = asyncEmitter();

//This method is called, when an "error" event is emitted!
event.on("error", function(error) {

// This is used to emits the "error" event and return it.
var asyncEmitter = function() {
        emitr.emit("error", new Error("Here something went wrong!"));
    return emitr;
The EventEmitter will help you all to write the event based Node modules and also your knowledge of EventEmitter will greatly affect your efficiency Node.js carrier.


I hope this post is helpful to you guys! Thank you!

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog -
My Books - Book 1 and Book 2 Powered by Blogger.