【发布时间】:2018-01-17 16:13:50
【问题描述】:
在遍历数组之前我需要等待一个异步函数完成,我需要等待解析的异步函数如下:
static async sendEmail (from, to, subject, text) {
return new Promise((resolve, reject) => {
let message = {
from,
to,
subject,
text
};
AMMailing.transporter.sendMail(message, function (err, response) {
if (err) {
reject(err);
} else {
resolve(response);
}
});
});
}
这是我在数组中迭代并尝试等待它在再次迭代之前解决的代码:
static sendEmailInQueue (queue) {
queue.map(async (person, index) => {
console.log('sending email to: ', person.email);
try {
let success = await AMMailing.sendEmail(AMMailing.message.from, person.email, AMMailing.message.subject, AMMailing.message.text);
if (success) {
console.log('email sent to: ', person.email);
}
} catch (err) {
console.log(err);
}
});
}
我的问题是:这条线 console.log('sending email to: ', person.email); 一直执行,然后 AMMailing.sendEmail() 函数开始记录它的结果
这是我在控制台中得到的输出:
sending email to: matheus.rezende10@gmail.com
sending email to: matheus.rezende10@gmail.com
sending email to: matheus.rezende10@gmail.com
sending email to: matheus.rezende10@gmail.com
sending email to: matheus.rezende10@gmail.com
sending email to: matheus.rezende10@gmail.com
sending email to: matheus.rezende10@gmail.com
sending email to: matheus.rezende10@gmail.com
sending email to: matheus.rezende10@gmail.com
sending email to: matheus.rezende10@gmail.com
sending email to: matheus.rezende10@gmail.com
{ Error: Hostname/IP doesn't match certificate's altnames: "Host: mail.appmasters.io. is not in the cert's altnames: DNS:*.sgcpanel.com, DNS:sgcpanel.com"
【问题讨论】:
-
这很可能不是
async/await的问题。您是否仅使用一位用户尝试过您的代码(没有映射 - 最简单的实现)并验证它是否有效? -
@spicypumpkin 是的,尝试了单项,它可以按预期工作,但多项就不行
标签: javascript arrays node.js asynchronous nodemailer