0

So I have this function to store data in Firestore database but I wanted to check if the value exists already and based on that I want to return a boolean. I kept trying to solve this with async await but it did not seem to work. Finally when I added a return before performanceRef.get(), it solved the issue. Even though it solved the issue I'm not clear why. I know it must have something to do with async. Can someone please explain why adding that return solved the issue?

export const createUserPerformanceDocument = async (user, currentDate, answer ) => {

  const createdAt = currentDate.toLocaleDateString('en-US');
  const performanceRef = firestore.doc(`performance/${user}`);
  return performanceRef.get()
    .then(doc => {
      if(doc.exists) {
        const docData = doc.data();
        if (docData[createdAt]) {
          return false
        } else {
          try {
            performanceRef.set({ 
              [createdAt]: {
                Question: answer
              }
            },  { merge: true })
            return true
          } catch(error) {
            console.log('Error creating performance data!', error.message);
            return false
          }
        }
      } else {
        console.log("This user does not exist in the database");
      }
    })

}
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
sayayin
  • 971
  • 5
  • 23
  • 36
  • Your async/await version was probably just doing the wrong thing, but since you're not showing it, it's impossible to know. As it stands now, you are not really using async/await at all the way it was intended. You could remove the async keyword and it would still work, because you are now just using normal promise syntax. – Doug Stevenson Aug 08 '20 at 22:57
  • @DougStevenson exactly the same code, just had await instead of return. – sayayin Aug 08 '20 at 23:33
  • Does this answer your question? [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) – Jared Smith Aug 09 '20 at 00:30

1 Answers1

0

It is not return what solved issue, probably there were some issues in your async/await code, and when you rewrote it into then callback it worked as expected.

Feel free to post your async/await version if you want more clear answer.

P.S. You don't use await keyword, so you don't need async modifier before function

Update from comments

Await keyword simply helps us stop function execution on current line and wait for promise to resolve. If you want to get result of the promise(it is named doc in your then callback) you need to store it in some constant:

const doc = await performanceRef.get()

So there you have it and can perform any kinds of validation like you do in then callback.

If you want to return validation result from this function, simply use return keyword like you already did.

Temoncher
  • 644
  • 5
  • 15
  • It is exactly the same code and right where you see the return I had await like this: await performanceRef.get() I just tried it again and when the function is called it returns undefined but if I add back the return, I start to receive a boolean. And yes I will remove that async. Thanks. – sayayin Aug 08 '20 at 23:32
  • Ok, I understand what are you struggling with. Await keyword simply helps us stop function execution on current line and wait for promise to resolve. If you want to get result of the promise(it is named `doc` in your then callback) you need to store it in some constant:`const doc = await performanceRef.get()` So there you have it and can perform any kinds of validation like you do in then callback. If you want to return validation result from this function, simply use return keyword like you already did. – Temoncher Aug 08 '20 at 23:39
  • Also it is a bad practice to mix `async/await` syntax with then callbacks, so chose one that you like and keep using it across whole project – Temoncher Aug 08 '20 at 23:41
  • aha so guess only thing that will change is const doc = await performanceRef.get() and add return doc at the end of the function. Tried this and seems to be working. – sayayin Aug 08 '20 at 23:47
  • Also, can you please update the answer above with this info and I will accept it.Thanks – sayayin Aug 09 '20 at 00:23