orliesaurus / nodemailer-mailgun-transport

nodemailer is an amazing node module to send emails within any of your nodejs apps. This is the transport plugin that goes with nodemailer to send email using Mailgun 🔫
MIT License
880 stars 97 forks source link

ES6 Syntax #78

Closed strix closed 5 years ago

strix commented 5 years ago

First to note: this syntactical update is fully backwards compatible with the original API. It doesn't use async/await since nodemailer supports node>=6.0.0 and async/await isn't fully integrated until node 7.

I've updated most everything to use new ES6 syntax and features and removed 4 dependencies in the process.

The previous series are now functions that are later called making it easier for future feature additions. To add a function that is synchronous, simply declare the function and call it in whatever order you deem necessary:

const synchronousFunction = () => {
  // function code here
}

// ...

convertAddressesToStrings();
transformMailData();
resolveAttachments();
synchronousFunction(); // or before/after any synchronous function
resolveTemplate()
  .then(sendMail)
  .then((data) => {
    callback(null, data);
  }).catch((err) => {
    callback(err);
  });

To add an asynchronous function or convert an existing function to by async (a good example for this would be converting resolveAttachments for handling file attachments as buffers or streams as seen here) add a function that returns a Promise and add it wherever in the promise chain:

const asynchronousFunction = () => {
  return new Promise((resolve, reject) => {
    // function body. make sure to call resolve/reject appropriately
  };
}

// ...

convertAddressesToStrings();
transformMailData();
resolveAttachments();
resolveTemplate()
  .then(asynchronousFunction) // or before/after any asynchronous function in the promise chain. However, `sendMail` must be last in the promise chain
  .then(sendMail)
  .then((data) => {
    callback(null, data);
  }).catch((err) => {
    callback(err);
  });