UNPKG

ca-apm-probe

Version:

CA APM Node.js Agent monitors real-time health and performance of Node.js applications

77 lines (60 loc) 1.66 kB
var id = 0; function Math() { ++id; } Math.prototype.add = function (x, y) { return x + y; } Math.prototype.subtract = function (x, y) { return x - y; } Math.prototype.compute = function (op, x, y, callback) { this.execute(op, x, y, callback); } Math.prototype.calculate = function (op, a, b) { var self = this; var executor = function (resolve, reject) { setTimeout(function () { var answer = 0; if (a < 0 || b < 0) { reject(new Error("Invalid Arguments: argument value should be >= 0")); return; } if (op === "add") { answer = self.add(a, b); } else if (op === "sub") { answer = self.subtract(a, b); } else { reject(new Error("Unsupported Operation!")); return; } resolve(answer); }, 1); }; return new Promise(executor); }; Math.prototype.execute = function (op, x, y, callback) { var self = this; setTimeout(function () { var answer = 0; if (x < 0 || y < 0) { callback(new Error("Invalid Arguments: argument value should be >= 0")); return; } if (op === "add") { answer = self.add(x, y); } else if (op === "sub") { answer = self.subtract(x, y); } else { callback(new Error("Unsupported Operation!")); return; } // now call the callback function callback(null, answer); }, 5) }; module.exports = Math;