j.doe
j.doe

Reputation: 4859

Return a value asynchronously with react native

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?

Upvotes: 2

Views: 99

Answers (1)

Andrew Paramoshkin
Andrew Paramoshkin

Reputation: 989

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

Upvotes: 1

Related Questions