I want to do a callback with the calculator if successful, and I want it to catch the "Too High" error. But now when it throws the error all i get is "Uncaught TypeError: callback is not a function". No idea what I am doing wrong here
const calculator = (number, callback) => {
setTimeout(() => {
try { if (number > 3) Error('Too high')
callback(null, number);
} catch (err) {
callback(err, null);
}
}, 888);
};
I hope you are passing a callback as a param when calling the calculator function. If you want to catch the error Too High what you can do here instead is add the condition to the callback function you're passing as a param to the calculator function and return it from there since here your if condition is returning undefined. Actually, instead of adding the if one-line condition, use the && operator in the callback function, you are passing to return an error if a number is greater than 3. And just like T.J Crowder is saying you use throw Error('Too High').