从第一个文件中调用一个函数,然后在第一个函数完成并返回true时执行其他操作的最佳方法是什么?
module.exports = {
sendEmail: function(user, subject, text) {
var smtpTransport = nodemailer.createTransport({
service: 'gmail',
auth: {
user: config.email,
pass: config.gmlpwd
}
});
var mailOptions = {
to: user,
from: config.email,
subject: subject,
text: text
};
smtpTransport.sendMail(mailOptions, function(err) {
return true
});
}
}
mailer.sendEmail(user.email, subject, text, function(){
//do something here if the function is done successfully, but it never gets called
});
答案 0 :(得分:1)
在sendMail
函数内部,您需要将该回调添加为参数,然后在完成后调用它。
module.exports = {
sendEmail: function(user, subject, text, cb) {
var smtpTransport = nodemailer.createTransport({
service: 'gmail',
auth: {
user: config.email,
pass: config.gmlpwd
}
});
var mailOptions = {
to: user,
from: config.email,
subject: subject,
text: text
};
smtpTransport.sendMail(mailOptions, function(err) {
return cb(err);
});
}
}
mailer.sendEmail(user.email, subject, text, function(){
// do something here
});
但是,反复执行此操作可能会导致您进入callback hell,建议您改用Promise
或RxJS。
答案 1 :(得分:1)
<!-- With cb -->
module.exports = {
sendEmail: function(user, subject, text, callback) {
var smtpTransport = nodemailer.createTransport({
service: 'gmail',
auth: {
user: config.email,
pass: config.gmlpwd
}
});
var mailOptions = {
to: user,
from: config.email,
subject: subject,
text: text
};
smtpTransport.sendMail(mailOptions, function(err) {
if (err) callback({ err })
callback({ result: "All is ok" })
});
}
}
//
var mailer = require('./file.js');
mailer.sendMail('go@mail.com', 'Hello world', 'Lorem ipsum dolor sit ammet', function({ err, result}){
if (err) {
console.log(err, '- Some error')
} else {
console.log(result, 'Some code here')
}
})
<!-- With Async promise -->
module.exports = {
sendEmail: new Promise(function(resolve, reject){
function(user, subject, text) {
var smtpTransport = nodemailer.createTransport({
service: 'gmail',
auth: {
user: config.email,
pass: config.gmlpwd
}
});
var mailOptions = {
to: user,
from: config.email,
subject: subject,
text: text
};
smtpTransport.sendMail(mailOptions, function(err) {
if (err) reject({ err })
resolve({ result: 'All is ok result here'})
});
}
}
}
//
var mailer = require('./file.js');
mailer.sendMail('go@mail.com', 'Hello world', 'Lorem ipsum dolor sit ammet').then(function ({ result}) {
console.log( 'All is ok')
})
答案 2 :(得分:0)
在这种情况下,我更喜欢使用async/await
,在这种情况下,您只需要将函数与对象一起进行导出即可,让我们看下面的示例。
文件1: userController.js
const userUtils = require('./userUtils');
const userCtr = {};
userCtr.searchUser = async (req, res) => {
try {
const { userName } = req.query;
const result = await userUtils.searchUser(userName);
return res.status(200).json(result);
} catch (err) {
return res.status(err.code).json({ error: err.error });
}
};
module.exports = userCtr;
文件2: userUtils.js
const userUtils = {};
userUtils.searchUser = async (userName) => {
try {
if (userName) {
// ...Do some cool stuff
const result = [];
return result;
}
const errorObj = { code: 400, error: 'ERR_VALID_PARAM' };
throw errorObj;
} catch (err) {
console.error(err);
throw err;
}
};
module.exports = userUtils;
在上述实例中,我们从searchUser()
调用了函数userUtils
,如果存在参数,该函数将返回一个空数组,否则将引发错误。这只是您了解module.exports