I am a newbie in JavaScript, currently delving into functions and promises. I have created a small code snippet with the following purpose: to comprehend how promise function (then
) works on a function that returns a value.
input: a,b
output: if the sum of a & b is 0, then display an alert statement, otherwise show the sum in the alert message.
const func = (a, b) => {
let operand1 = a * 10;
let operand2 = b * 10;
return operand1 + operand2
};
const funcwrapper = (a, b) => {
func(a, b).then((sum) => {
if (sum == 0) {
window.alert("value is zero");
} else {
window.alert(("sum is " + sum));
}
})
};
funcwrapper(5, 5);
Despite reading extensively on promises on functions with return values, I still find it challenging. Therefore, I decided to implement this code to gain a better understanding but encountered some errors along the way.
If you can offer guidance on this topic, I would greatly appreciate it.
Your assistance is valued. Thank you in advance