私は初心者で、現在NodeJSでUser Managementシステムを作っています。以前はMongoDB、Expressでそれをやっていました。今、Express、Sequelize、Postgresqlでいくつかの概念をよりよく理解するために、すべてを再作成しています。nodejsで約束を使って非同期コールバックを変更する方法は?
私が以前にAsync.waterfallを使用して電子メールIDを取得し、SendGridを使用して電子メールを送信したリセットページがありましたが、どのように私が約束を使用して変換することができるか知りたいですか?同時コールバックでそれらを使用する方法を理解するのはちょっと混乱します。ここ
はasync.waterfallを使用して、以前のコードである:async.waterfallドキュメントから
app.post('/forgotpassword', function(req, res, next) {
async.waterfall([
function(done) {
crypto.randomBytes(20, function(err, buf) {
var token = buf.toString('hex');
done(err, token);
});
},
//2
function(token, done) {
User.findOne({ 'local.email': req.body.email }, function(err, user) {
if (!user) {
req.flash('forgotMessage', 'No account with that email address exists.');
return res.redirect('/forgotpassword');
}
user.local.resetPasswordToken = token;
user.local.resetPasswordExpires = Date.now() + 3600000; // 1 hour
user.save(function(err) {
done(err, token, user);
});
});
},
//3
function(token, user, done) {
var nodemailer = require('nodemailer');
var sgTransport = require('nodemailer-sendgrid-transport');
var options = {
auth: {
api_key: ''
}
};
var mailer= nodemailer.createTransport(sgTransport(options));
var mailOptions = {
to: user.local.email,
from: 'p[email protected]',
subject: 'Node.js Password Reset',
text: 'You are receiving this because you (or someone else) have requested the reset of the password for your account.\n\n' +
'Please click on the following link, or paste this into your browser to complete the process:\n\n' +
'http://' + req.headers.host + '/reset/' + token + '\n\n' +
'If you did not request this, please ignore this email and your password will remain unchanged.\n'
};
mailer.sendMail(mailOptions, function(err) {
req.flash('forgotMessage', 'An e-mail has been sent to ' + user.local.email + ' with further instructions.');
done(err, 'done');
});
}
],
//2 out of Async
function(err) {
if (err) return next(err);
res.redirect('/forgotpassword');
});
});
Promise.all([]) –
を使用すると、@UditKumawatは私の構造をどのようにするのでしょうか?私は約束を使用している場合、私もdone()を使わなければならないのだろうかと混乱してしまいます。あなたは単に例を挙げて説明できますか?助けをたくさんお願いします。 –
@UditKumawatいいえ、彼は傾けることができます。なぜなら、function2はfunction1の結果を要求し、function3はfunction2の結果を必要とします。 promise.all execは同時に約束し、promiseArr内の各関数は別のexecに依存することはできません。 – styopdev