0

I have this value which I obtained from the fetch request which is outside my calling function scope. How can i compare this against a a value.

let h=isCodeExists(userInput).then(res => console.log(res));

I want to check if this value ==2

sruthy123
  • 11
  • 3

1 Answers1

1

As the response will only come later, you need to make the comparison also later, i.e. it should be done in the then callback:

isCodeExists(userInput).then(res => {
    console.log(res);
    if (res == 2) console.log("it is 2!!");
    // Any other logic that needs the value of `res` should come here...
});
trincot
  • 317,000
  • 35
  • 244
  • 286