title: Nodejs Function is returning undefined
body: <p>I have 2 files index.js and models.js</p>
  <p>my index.js file requires the model.js file</p>
  <pre><code>const models = require(&quot;./modules/models.js&quot;);
  
  app.post('/message', (req, res) =&gt; {
      const _lead = models.createLead(req.body)
      _lead.then(l =&gt; {
          console.log(&quot;resulted in lead&quot;, JSON.stringify(l))
          // GETTING UNDEFINED HERE
      })
      // const _message = await models.createMessage(req.body.message)
      res.send();
  });
  </code></pre>
  <p>My models.js file has a functions that contain Promises</p>
  <pre><code>const createLead = async function (payload) {
      // Find lead
      let result
      new Promise((resolve, reject) =&gt; {
          findLeadById(payload.lead.rid).then(async _lead =&gt; {
              // If a lead has not been found
              if (!_lead) {
                  if(payload.companyRid != payload.lead.rid){
                  await db.collection('leads').add(payload.lead).then((l) =&gt; {
                      result = payload.lead
                      result['id'] = l.id
                  })
              }
              } else {
                  result = _lead
              }
          }).then(() =&gt; {
              console.log(&quot; result of create lead &quot;,JSON.stringify(result))
              resolve(result)
          }).catch(error =&gt; {
              error.log(&quot;reject&quot;, error)
              reject(error)
          })
      })
  }
  
  const findLeadById = async function (lead) {
      new Promise(async (resolve, reject) =&gt; {
          const ref = db.collection('leads');
          console.log(&quot;finding lead&quot;, JSON.stringify(lead))
          await ref.where(&quot;rid&quot;, &quot;==&quot;, lead).get().then((s) =&gt; {
              
              let obj = null
              if(s){
                  s.forEach(doc =&gt; {
                      console.log(&quot;found the lead in loop&quot;, JSON.stringify(doc.data()))
                      obj = doc.data()
                      obj['id'] = doc.id
                  })
              }
              resolve(obj)
          }).catch(error =&gt; {
              error.log(&quot;reject&quot;, error)
              reject(error)
          })
      })
  }
  
  exports.createLead = createLead;
  exports.findLeadById = findLeadById;
  </code></pre>
  <p>The model.js returns an object as expected</p>
  <pre><code>console.log(&quot; result of create lead &quot;,JSON.stringify(result))
  </code></pre>
  <p>However, I'm expecting to see that same object in the index.js in the then block but I'm getting undefined.</p>
  <pre><code>console.log(&quot;resulted in lead&quot;, JSON.stringify(l))
  // GETTING UNDEFINED HERE
  </code></pre>
  <p>The following is not waiting for the associated functions to finish</p>
  <pre><code>const _lead = models.createLead(req.body)
  </code></pre>
  <p>I have also tried adding async and await which also didn't work</p>
  <pre><code>const _lead = await models.createLead(req.body).then(res =&gt; {console.log(&quot;result&quot;, res) // Still undefined
  })
  </code></pre>