title: node.js async promises sequential execution
body: <p>I am trying to write a Firebase functions using node.js</p>
  
  <p>In the below code "pushNotifications" function is triggered when there is new entry in the messagesDB. </p>
  
  <p>From the messagesDB i am getting the email from the messages dictionary.</p>
  
  <ol>
  <li>Calling the function getUserID() with email address to fetch the UID</li>
  <li>UID collected is then pass it to the getUserStatus(uid) and get the current status which returns a bool.</li>
  </ol>
  
  <p>i will not be able to get the status of the user without his uid, and needed to executed sequentially.</p>
  
  <p>(I can execute them individually using hardcoded values, but not sequentially)
  Any help will appretiated.</p>
  
  <pre><code>function getUserStatus(uid) {
  
    var onlineStatus = true;
    console.log('getting the status.........', uid);
    return firestoreDB.collection('userstatus').doc(uid).get()
      .then(doc =&gt; {
        if (doc.exists) {
          onlineStatus = doc.data()['online'];
        }
        return onlineStatus;
      })
      .catch(err =&gt; {
        return err;
      });
  }
  
  function getUserID(toId) {
    var uid;
    console.log('get uid function....');
    return admin.auth().getUserByEmail(toId)
      .then((userRecord) =&gt; {
        console.log('record :', userRecord);
        uid = userRecord.uid
        console.log('uid :', uid);
        console.log(`getting document`);
        return uid;
      })
      .catch((error) =&gt; {
        console.log("Error fetching getUserID :", error);
        return err;
      })
  }
  
  
  exports.pushNotifications = functions.database.ref('/Messages/{messageID}').onWrite(event =&gt; {
  
    console.log('event Data : ', event.after._data);
    const data = event.after._data;
    const fromId = data.from;
    const toId = data.to;
    const message = data.messageText;
    console.log(fromId + ' sent a message to ' + toId);
    console.log('getting uid..');
  
    const statusOnline = getUserID(toId)
    .then(uid =&gt; {
                  console.log('got uid', uid); 
                  getUserStatus(uid)
                })
    .then(status =&gt; {return status})
    .catch(err =&gt; console.log('Error executing the promises', err.message));
    console.log('statusOnline : ', statusOnline);
    console.log('Done.......');
  });
  </code></pre>