Skip to content

Instantly share code, notes, and snippets.

@Sysetup
Created October 10, 2016 23:51
Show Gist options
  • Select an option

  • Save Sysetup/a81ad4755f33595f774c28b2e831a558 to your computer and use it in GitHub Desktop.

Select an option

Save Sysetup/a81ad4755f33595f774c28b2e831a558 to your computer and use it in GitHub Desktop.
Callback example
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);
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