[英]Speed up Nodemailer send Emails
我有一個類似這樣的 NodeJS 代碼:(Netlify)
const sendEmail = require('./sendmail')
async function checkout() {
try {
await someFunction()
} catch(e) {}
try {
await someOtherFunction()
} catch(e) {}
try {
await sendEmail.sendCustomer(subject, body, to) // await or not?
await sendEmail.sendOurself(subject, body, to) // await or not?
}
finally {
return {
statusCode: 200
}
}
}
和 function 發送電子郵件:
const nodemailer = require('nodemailer')
async function sendEmail(subject, body, to) {
const transporter = nodemailer.createTransport({
host: HOST,
port: PORT,
secure: SECURE,
auth: { user: AUTH.user, pass: AUTH.pass,}
});
const mailOptions = {
from: AUTH.user,
to: to,
subject: subject,
html: body,
};
return await transporter.sendMail(mailOptions)
};
exports.sendEmail = sendEmail
問題是,function 發送電子郵件需要很長時間。 有時 5 秒,有時兩封電子郵件長達 12 秒。
我怎樣才能加快速度? 如您所見,我並不真正關心來自 nodemailer 的響應。 所以我嘗試在最后一個 try 塊中刪除 await 並使 function sendEmail 同步。 但是,電子郵件沒有被發送。 我認為這是因為當我返回 200 時 function 停止運行。我怎樣才能加快這個過程?
我最終得到了 Promise.all 而不是等待。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.