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.

Example,
//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) {
    console.error(error);
   //console.trace(error);
});

// This is used to emits the "error" event and return it.
var asyncEmitter = function() {
    process.nextTick(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.

References,


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

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
http://www.code-sample.com
http://www.code-sample.xyz

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.