title: Async function is returning undefined
body: <p>I really need to brush up on my async await and promises. I would love some advice. </p>
  
  <p>I'm making an async function call to firebase firestore. The function should  return a string depending on a single input param. </p>
  
  <p>The feature is for a 1-1 user chat. 
  The function is to create the chat/find existing chat, and return its ID. </p>
  
  <p>Right now, I am getting <code>undefined</code> as the return value of <code>openChat</code> and can't work out why. The function otherwise works, apart from the return. </p>
  
  <p>I have two functions. One is a React class component lifecycle method, the other my firebase async function. </p>
  
  <p>Here is the class component lifecycle method:</p>
  
  <pre><code>async getChatId(userId) {
    let chatPromise = new Promise((resolve, reject) =&gt; {
      resolve(openChat(userId))
    })
    let chatId = await chatPromise
    console.log('chatId', chatId) //UNDEFINED
    return chatId
  }
  
  async requestChat(userId) {
    let getAChat = new Promise((resolve, reject) =&gt; {
      resolve(this.getChatId(userId))
    })
    let result = await getAChat
    console.log('result', result) //UNDEFINED
  }
  
  render() {
    return (&lt;button onClick = {() =&gt; this.requestChat(userId)}&gt;get id&lt;/button&gt;)
  }
  </code></pre>
  
  <p>and here is the async function:</p>
  
  <pre><code>// both my console.log calls show correctly in console
  // indicating that the return value is correct (?)
  
  export async function openChat(otherPersonId) {
    const user = firebase.auth().currentUser
    const userId = user.uid
  
    firestore
      .collection('users')
      .doc(userId)
      .get()
      .then(doc =&gt; {
        let chatsArr = doc.data().chats
  
        let existsArr =
          chatsArr &amp;&amp;
          chatsArr.filter(chat =&gt; {
            return chat.otherPersonId === otherPersonId
          })
        if (existsArr &amp;&amp; existsArr.length &gt;= 1) {
          const theId = existsArr[0].chatId
  
          //update the date, then return id
  
          return firestore
            .collection('chats')
            .doc(theId)
            .update({
              date: Date.now(),
            })
            .then(() =&gt; {
              console.log('existing chat returned', theId)
              //we're done, we just need the chat id
              return theId
            })
        } else {
          //no chat, create one
  
          //add new chat to chats collection
          return firestore
            .collection('chats')
            .add({
              userIds: {
                [userId]: true,
                [otherPersonId]: true
              },
              date: Date.now(),
            })
            .then(docRef =&gt; {
              //add new chat to my user document
  
              const chatInfoMine = {
                chatId: docRef.id,
                otherPersonId: otherPersonId,
              }
              //add chat info to my user doc
              firestore
                .collection('users')
                .doc(userId)
                .update({
                  chats: firebase.firestore.FieldValue.arrayUnion(chatInfoMine),
                })
  
              //add new chat to other chat user document
              const chatInfoOther = {
                chatId: docRef.id,
                otherPersonId: userId,
              }
              firestore
                .collection('users')
                .doc(otherPersonId)
                .update({
                  chats: firebase.firestore.FieldValue.arrayUnion(chatInfoOther),
                })
              console.log('final return new chat id', docRef.id)
              return docRef.id
            })
        }
      })
  }
  </code></pre>
  
  <p>If you have any useful tips whatsoever, I would be forever grateful to hear them!</p>
  
  <p>Expected results are a returned string. The string is correctly displayed the console.log of the async function). </p>
  
  <p>Actual results are that the return value of the async function is undefined.</p>