how can I get a ‘then’, or asynchronously return an API response?
I use fastify, but it doesn’t wait for a response if you make a callback inside.
I tried that, but the error: TypeError: a.then is not a function
const a = await transporter.sendMail(mainOptions);
a.then((error, result) => {
if (error) return error
reply.send({
messageId: result.messageId
})
})
>Solution :
Simply log a:
const a = await transporter.sendMail(mainOptions);
console.log(a)
You can also catch the error with a try/catch
try {
const a = await transporter.sendMail(mainOptions);
console.log(a)
reply.send({ messageId: result.messageId })
} catch (error) {
console.error(error)
}
Looks like you are trying to send email with nodemailer. Have you tried to follow the documentation:
transporter.sendMail({
from: 'sender@example.com',
to: 'recipient@example.com',
subject: 'Message',
text: 'I hope this message gets delivered!'
}, (err, info) => {
console.log(info.envelope);
console.log(info.messageId);
});