Search code examples
node.jstypescriptasync-awaites6-promisenodemailer

converting callback to async throws error


The following code works:

  var smtpConfig = {
    host: 'localhost',
    port: 465,
    secure: true, // use SSL
    selfSigned: true
  };

  // create reusable transporter object using the default SMTP transport
  var transporter = nodemailer.createTransport(smtpConfig);

  // setup e-mail data with unicode symbols
  var mailOptions = {
    from: '"Some One" <[email protected]>', // sender address
    to: '[email protected]', // list of receivers
    subject: 'Hello', // Subject line
    text: 'Hello world ?', // plaintext body
    html: '<b>Hello world ?</b>' // html body
  };

  transporter.sendMail(mailOptions, (error, info) => {
      if (error) {
          console.log(error);
      } else {
          console.log(info);
      }
  });

However, if I use util.promisify from node

  var sendMail = promisify(transporter.sendMail);
  var info = await sendMail(mailOptions);

I get an exception

TypeError: Cannot read property 'getSocket' of undefined
  at sendMail (c:\Users\user\Source\project\node_modules\nodemailer\lib\mailer\index.js:143:25)
  at sendMail (internal/util.js:230:26)

problem is inside sendMail because 'this' is undefined:

/**
 * Sends an email using the preselected transport object
 *
 * @param {Object} data E-data description
 * @param {Function?} callback Callback to run once the sending succeeded or failed
 */
sendMail(data, callback) {
    let promise;

    if (!callback && typeof Promise === 'function') {
        promise = new Promise((resolve, reject) => {
            callback = shared.callbackPromise(resolve, reject);
        });
    }

    if (typeof this.getSocket === 'function') { <-- this is undefined
        this.transporter.getSocket = this.getSocket;
        this.getSocket = false;
    }

using util.promisify on plain functions works. I suppose this is because I use util.promisify on a class method.

Is there a way to achive this or do I need to extend the exting class with an async function sendMail overload


Solution

  • To solve this particular case, you can use

    await sendMail.call(transporter, mailOptions) // provide a context
    

    Of course, looking at the source for transporter.sendMail(), it already supports invoking as a thenable without wrapping in util.promisify(), so you can await it directly:

    await transporter.sendMail(mailOptions) // omit callback to kick in promise-style async