Nodemailer不会退出邮件

在我的脚本中,我想用一个电子邮件发送有关使用Nodemailer的脚本运行的数据。 在脚本的中间使用相同的transporter器,并成功发送电子邮件。 但是,退出事件处理程序中的相同代码根本不会执行任何操作。 没有电子邮件,没有“发送报告!” 输出到控制台,虽然我得到的输出到控制台的mailOptionvariables。 假设asynchronous操作在退出之前没有完成,我添加了一个临时延迟步骤来validation这一点。 还没有生成电子邮件。 难道我做错了什么?

 function exitHandler(er) { end = moment().utc(); // report template var tplPath = path.resolve('report.html'); var juiceFileDeAsync = deasync(juice.juiceFile); var tpl = juiceFileDeAsync(tplPath, { webResources: {images: false} }); var email = nunjucks.renderString(tpl, { startDate: start.format("MMMM Do YYYY, h:mm:ss a"), duration: end.diff(start,'seconds'), report: sentEmails, warns: warns, errors: errors }); // email report mailOptions.to = '<valid email>'; mailOptions.html = email; transporter.sendMail(mailOptions, function(error, info) { console.log('Report sent!', error, info); }); console.log(mailOptions); if (er) { console.log(er); } var e = new Date().getTime() + (10 * 1000); while (new Date().getTime() <= e) { ; } } // do something when app is closing process.on('exit', exitHandler); process.on('uncaughtException', exitHandler); 

https://nodejs.org/api/process.html看看Event:exit文档&#xFF1A;

 process.on('exit', function(code) { // do *NOT* do this setTimeout(function() { console.log('This will not run'); }, 0); console.log('About to exit with code:', code); }); 

一旦所有退出监听器完成运行,进程将退出。 因此,您只能在此处理程序中执行同步操作。