title: Convert a method into a promise : Nodejs
body: <p>I have the following Nodejs method that I want to call and execute a <code>.then</code> function on. I am not sure how to write this method so that I can use the <code>.then()</code> method on it.</p>
  
  <p>I have the <code>sendEmail</code> method in <code>nodemailer.js</code> that I want to call in my <code>app.js</code> and execute the <code>.then()</code> method on. It currently gives me the error: </p>
  
  <blockquote>
    <p>nodeMailer.sendEmail().then(function ...<br>
    TypeError: Cannot read property 'then' of undefined.</p>
  </blockquote>
  
  <p>I understand this is because my method does not return a promise. I am not sure how to write this method to return a promise while it retains its original functionality.</p>
  
  <h2>nodemailer.js</h2>
  
  <pre><code>var nodemailer = require('nodemailer');
  
  var transporter = nodemailer.createTransport('smtps://********');   
  var mytext="my new text";
  
  var mailOptions ={
      from: '"Reviews"&lt;Reviews@reviews.com&gt;', // sender address
      to: 'Someone@something.com', // list of receivers
      subject: 'Test', // Subject line
      text: '', // plaintext body
      }
  
  module.exports = {
      mailOptions:mailOptions,
      sendEmail: function() {
          transporter.sendMail(mailOptions, function(error, info) {
              if (error) {
                      console.log(error);
                 } else {
                      console.log('Message sent: ' + info.response);
                 }
          })
      }
  }
  </code></pre>
  
  <p>Calling the above in <code>app.js</code> gives the error mentioned above</p>
  
  <h2>app.js</h2>
  
  <pre><code>var nodeMailer = require('./nodeMailer')
  var cron = require('cron');
  
  var cronJob1 = cron.job("*/30 * * * * *", function(){
      Model.find(query,function (err, results) {
          if (err) {
              res.status(500).json({ message: 'Something Broke!' });
          } else {
              nodeMailer.mailOptions.text =  JSON.stringify(results);
              nodeMailer.sendEmail().then(function(){
                  Model.findAndUpdate(query,  { $set: { "email": true }}, 
                      function(err, resp){
                          console.log(resp, "successfully updated")
                      })
              });
          }
      });
  }); 
  cronJob1.start();
  </code></pre>