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

async await syntax doesn't work #267

Closed
websocket98765 opened this issue Jul 18, 2020 · 3 comments · Fixed by #275
Closed

async await syntax doesn't work #267

websocket98765 opened this issue Jul 18, 2020 · 3 comments · Fixed by #275

Comments

@websocket98765
Copy link

Just a heads up that this doesn't send email successfully when using async/await syntax. The code runs through as if it succeeded (doesn't throw an error), but the email is not sent and result logs as undefined.

  try {
    const result = await client.send(message);
    console.log('Email sent successfully', result);
  } catch (err) {
    console.error(err);
  }

The callback style in the docs works, of course.

  client.send(message, (err, message) => {
    if (err) {
      console.error(err);
    } else {
      console.log('Email sent successfully', message);
    }
  });

But it might be good to support both.

@zackschuster
Copy link
Collaborator

the current version of the library is (mostly) written in the old "errback" style, so for now the recommendation is to use util.promisify and/or custom Promise wrappers for async/await workflows.

i'm not opposed to switching to a Promise-based interface in a future release. definitely open to a PR for this.

@zackschuster
Copy link
Collaborator

zackschuster commented Jul 27, 2020

code snippet for util.promisify with async/await:

const client = new SMTPClient({});
// bind necessary to retain internal access to client prototype
const sendAsync = util.promisify(client.send.bind(client));

try {
  const result = await sendAsync(message);
} catch (err) {
  // handle err
}

@eleith
Copy link
Owner

eleith commented Dec 1, 2020

thanks @zackschuster lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants