I am calling this function called numberOfRedeems(dealId) from another function called setUpData. Note that the numberOfRedeems() function has a promise and returns the "counter". 
When I run the same function from the setUpData function it always comes undefined. Please tell me why this is happening. Would be highly appreciated!
I suspect the problem is that there is a promise in the function that im calling.
function getNumberOfRedeems(dealId){
    userId = firebase.auth().currentUser.uid;
    var counter = 0;
    db.collection("redeemedDeals").get()
        .then(function(querySnapshot){
          querySnapshot.forEach(function(doc) {
          var data = doc.data();
          var docDealID = data.deal;
          console.log(docDealID);
          if (dealId == docDealID){
              counter = counter + 1;
          }
        });
      }).then(function(){
        console.log(counter);
        return counter;
      })
  }
function setUpData(){
    $("#uploadingProgress").hide();
    var user = firebase.auth().currentUser
    var userId = user.uid
    var userRef = db.collection('partners').doc(user.uid);
    return userRef
      .get()
      .then(doc => {
        if (doc.exists) {
          try{
          userName = doc.get("name")
          $("#partnerName").show().text(userName);
        }catch(error) {
          console.error("Could not retrieve ", error);}
        }
        db.collection("partners").doc(userId).collection("dealHistory").get()
        .then(function(querySnapshot) {
            querySnapshot.forEach(function(dealHistoryDocRef) {
                db.collection("deals").doc(dealHistoryDocRef.id).get().then(function(doc){
                      var dealId = dealHistoryDocRef.id
                      table.innerHTML +=  "<td><a id="+doc.get("id")+">"+doc.get("name")+"</a></td><td>" + getNumberOfRedeems(dealId) + "</td><td>"+doc.get("id")+"</td>"
                })
             });
        });
  })}
I expect the it to return a number, but its always undefined.
 
     
     
    