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

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 Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.