Tuesday, April 21, 2015

Callback Function

Read more details about Callback Function from the following web site:

Couple of lines from the above site "A callback function, also known as a higher-order function, is a function that is passed to another function (let's call this other function "otherFunction") as a parameter, and the callback function is called (or executed) inside the otherFunction."

Read more...

http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/



Promises

"Jo vaadaa kiya voo nibhana padegaa"

How people explain things in simple words so that many others can understand? I am really thankful to Sandeep Panda for explaining JavaScript Promises in simple English!

Couple of lines from this site "If you use the Promise API to make an asynchronous call to a remote web service you will create a Promise object which represents the data that will be returned by the web service in future...".

Read more...

http://www.sitepoint.com/overview-javascript-promises/



Sunday, April 5, 2015

module.exports in Node.js

Suppose you want to write a module, say a functions module and access it in another module, say main.js, how are you going to do it in Node.js?

Real simple!

Create two .js files under C:\nodetest folder: functions.js, main.js

functions.js
module.exports = { addNumbers : function() { var total = 0; for (var a = 0; a < arguments.length; a++){ total += parseInt(arguments[a]); } return total; }, findArea : function(r){ return 3.142 * r * r; } };


or 


functions.js:
this.addNumbers = function() { var total = 0; for (var a = 0; a < arguments.length; a++){ total += parseInt(arguments[a]); } return total; }; this.findArea = function(r){ return 3.142 * r * r; };

main.js
var functions = require('./functions.js'); console.log(functions.addNumbers(1,2)); console.log(functions.findArea(2));

Run the main.js from command prompt!

C:\nodetest>node main

Reference:

http://www.sitepoint.com/understanding-module-exports-exports-node-js/