2

I created a function in react-native that looks like this one

static async myFunction(){
  await RNSFAuthenticationSession.getSafariData(url,scheme).then(async (callbackUrl) => {
    const isValid = await MyClass.checkIfValid(data);
    if (isValid) {
      return true;
    } else {
      return false;
    }
}

and I am calling it in this way

const isValid = await MyClass.myFunction();
alert(isValid); // undefined

isValid contains an undefined value. Do you know how can I fix this?

Kraylog
  • 7,383
  • 1
  • 24
  • 35
j.doe
  • 4,559
  • 6
  • 20
  • 31
  • `await` does not automatically make a callback method into a Promise, you will need to make `getSafariData` into a Promise. – Keith Jan 22 '18 at 13:02
  • Possible Duplicate [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Mayank Shukla Jan 22 '18 at 13:05

1 Answers1

1

You forgot to return Promise

static async myFunction(){
   return await RNSFAuthenticationSession.getSafariData(url,scheme).then(async (callbackUrl) => {
   const isValid = await MyClass.checkIfValid(data);
   if (isValid){
      return true;
   } else {
      return false;
   }
}

and you can simplify your code

static myFunction() {
   return RNSFAuthenticationSession.getSafariData(url, scheme)
      .then(() => MyClass.checkIfValid(data))
}