Created
October 10, 2016 23:51
-
-
Save Sysetup/a81ad4755f33595f774c28b2e831a558 to your computer and use it in GitHub Desktop.
Callback example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var rect = require('./module'); | |
| function solveRect(l,b) { | |
| console.log("Solving for rectangle with l = " + l + " and b = " + b); | |
| rect(l,b, function(err,rectangle) { | |
| if (err) { | |
| console.log(err); | |
| } | |
| else { | |
| console.log("The area of a rectangle of dimensions length = " + l + " and breadth = " + b + " is " + rectangle.area()); | |
| console.log("The perimeter of a rectangle of dimensions length = " + l + " and breadth = " + b + " is " + rectangle.perimeter()); | |
| } | |
| }); | |
| }; | |
| solveRect(-3,5); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module.exports = function(x,y,callback) { | |
| try { | |
| if (x < 0 || y < 0) { | |
| throw new Error("Rectangle dimensions should be > 0: l = " + x + ", and b = " + y); | |
| } | |
| else | |
| callback(null, { | |
| perimeter: function () { | |
| return (2*(x+y)); | |
| }, | |
| area:function () { | |
| return (x*y); | |
| } | |
| }); | |
| } | |
| catch (error) { | |
| callback(error,null); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment