Skip to main content

Posts

Showing posts with the label node js error first callback

What is module in node.js?

A module is a just reference to the current module. A module encapsulates related code into a single unit of code. When creating a module, this can be interpreted as moving all related functions into a file. Each module will be placed in a separate .js file under a separate folder path. The module.exports property or the exports object allows us a module to select what should be shared with the application. The default present modules are installed when you are downloading and install in the node.js. In the node.js module.exports is the same as exports object. There are three types of modules, 1.       Core modules 2.       Local modules 3.       3 rd Party modules Node.js is a light weight framework. The core modules include bare minimum functionalities of Node.js. The list of the core modules, a)       http, b)      U...

Node.js EventEmitter - Error Handling

What is callback? What is error first callback? 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 Er...

Node.js Error First Callback - Error Handling

First I would like to share about “ What is callback? ” and after share about “ Error first callback ” in Node.js. A callback is an asynchronous function which is being called when an Ajax request/call is completed. The Node.js is using much more callback because the entire node APIs use it. What is “Error First Callback” in Node.js? The “ Error first callback ” is used to pass an error and data. The first argument to these functions is an error object and the second argument represents to the success data. So, you can check the first argument as an error object and the second argument as data. If nothing wrong happen in your app use data. Example, var successCallback = function (callback) { this._get( function (error, data) { if (error) { console .log (error); return ; } else { callback( null , data); } }); }; The error first callback pattern just requires that your function accepted two parameters and the first...