Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update quickstart example to include promises (for use in Vercel) #1292

Open
j2is opened this issue Aug 31, 2021 · 1 comment
Open

Update quickstart example to include promises (for use in Vercel) #1292

j2is opened this issue Aug 31, 2021 · 1 comment
Labels
status: help wanted requesting help from the community type: docs update documentation change not affecting the code

Comments

@j2is
Copy link

j2is commented Aug 31, 2021

Issue Summary

Within a serverless function such as Vercel it's nice to encapsulate the sending function within a promise to ensure that it completes before the job is terminated. Also checking for an API key in case it's not added by mistake

Code Snippet

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);

if (!process.env.SENDGRID_API_KEY) {
  throw new Error("Missing API key");
}

const msg = {
  to: '[email protected]',
  from: '[email protected]', // Use the email address or domain you verified above
  subject: 'Sending with Twilio SendGrid is Fun',
  text: 'and easy to do anywhere, even with Node.js',
  html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};

const sendMail = (msg) => {
  return new Promise((resolve, reject) => {
    sgMail
      .send(msg)
      .then(response => {
        resolve(response);
      }, error => {
        reject(error);
      });
    });
};

//ES6
sendMail(msg).then(() => {}, error => {
  console.error(error);

  if (error.response) {
    console.error(error.response.body)
  }
});

//ES8
(async () => {
  try {
    await sendMail(msg);
  } catch (error) {
    console.error(error);
    if (error.response) {
      console.error(error.response.body)
    }
  }
})();

Technical details:

  • sendgrid mail: ^7.4.6
  • node version: 14.x
@eshanholtz
Copy link
Contributor

This issue has been added to our internal backlog to be prioritized. Pull requests and +1s on the issue summary will help it move up the backlog.

@eshanholtz eshanholtz added status: help wanted requesting help from the community type: docs update documentation change not affecting the code labels Sep 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted requesting help from the community type: docs update documentation change not affecting the code
Projects
None yet
Development

No branches or pull requests

2 participants