python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
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

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions