rak
rak

Reputation: 61

TypeError: func(...).then is not a function

I am a beginner of JavaScript. I am trying to understand the functions and promises in JS. I wrote a small code. code Purpose: I wrote this code to understand the working of promise function(then) on function with a return value. input: a,b output: if the sum of a & b is 0, then show the alert statement else show sum in the alert value.

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);

I am still confused with this promise on functions with some return value after going through lot of investigation. So finally tried to implement the code to understand and got stuck with this error.

Please help me in understanding this topic.

Your help is appreciated. Thanks in advance

Upvotes: 1

Views: 5071

Answers (1)

Barmar
Barmar

Reputation: 781028

func() needs to return a promise:

const func = (a, b) => {

  let operand1 = a * 10;
  let operand2 = b * 10;

  return new Promise((resolve, reject) => resolve(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);

Upvotes: 7

Related Questions