1

when i run code get this error:

cant find variable: flagData

how can return(pass) data from promises implement(i use promises in another function)

resultOTP = OTP.fetchData(uniqueId, mobileNumber)

    .then((data) => {

      data.map((data)=>{
        if(data.message == "-1"){

          var flagData = 0
        }
        else {
          var flagData = 1
        }
      })


    })


    alert(flagData)
Alexander Danilov
  • 3,038
  • 1
  • 30
  • 35
codex
  • 87
  • 1
  • 12

1 Answers1

1

Firstly the variable is not defined in the right scope and even if it was it wouldn't have worked since you have an async call in which you are a changing the value, you might as well use async await like

async function fetchData() {
  const response = await OTP.fetchData(uniqueId, mobileNumber);
  let flagData = -1;
  if(response) {
     response.map((data)=>{
        if(data.message == "-1"){
          flagData = 0
        }
        else {
          flagData = 1
        }
      })
  }
  alert(flagData)
}
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400