0

I'm trying to return a boolean from an async function, but is is coming out as undefined.

checkIfEmptyDb = async => {
    var ref = firebase.database().ref("dynamicDb");
    ref.once("value").then(snapshot => {
      const a = snapshot.exists();
      console.log(a); // false
      return a;
    });
  };

  getRandomWordFromDb = async () => {
    let moreWords = await this.checkIfEmptyDb();
    console.log("moreWords", moreWords); //UNDEFINED

My solution so far is to set the state of the react app in the checkIfEmptyDbfunction, which then can be called in getRandomWordFromDb..

Thanks in advance!

1
  • forgot to add a return here: return ref.once("value").then()... Commented Mar 29, 2020 at 12:13

1 Answer 1

1

You need to return the value of the promise:

  checkIfEmptyDb = async => {
    var ref = firebase.database().ref("dynamicDb");
    return ref.once("value").then(snapshot => {
      const a = snapshot.exists();
      console.log(a); // false
      return a;
    });
  };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.