gpt4 book ai didi

node.js - 使用 async wait 后代码非常慢?

转载 作者:太空宇宙 更新时间:2023-11-03 23:55:19 25 4
gpt4 key购买 nike

这里使用 async wait ,但是当我向此发送请求时,它需要更多时间来发布数据,它变得非常慢?

user.user_type = 'admin';
user.hotelCode = hotelCode;
user.lastName = ''
user.setPassword(req.body.password);
const _id = hotelCode;
const hotelObj = { _id, email, created_by, country, state, city };
const hotelDb = new Hotel(hotelObj);
await hotelDb.save();
const saveUser = await user.save();
let token = saveUser.generateJwt();
const tokenDb = new tokenSchema({ _userId: saveUser._id, hotelCode:hotelCode, token: crypto.randomBytes(16).toString('hex') });
const tokenSave = await tokenDb.save();
await User.update({ email: user.email }, { $set: { token: tokenDb.token } }, { strict: false });
const mailOptions = {
from: 'xxxx@gmail.com',
to: saveUser.email,
subject: 'Account Verification Token',
html: '<p>Hello,</br>\n\n' + 'Please verify your account by clicking the link: \nhttp:\/\/'
+ req.headers.host + '\/api/account/verify\/' + '?token=' +
tokenSave.token + '&email=' + saveUser.email + '\n </p>'
};
await transporter.sendMail(mailOptions);
let restaurant = new Restaurant({ userId: saveUser._id, hotelCode: hotelCode, name: req.body.name.split(/[\s,]+/)[0] + "'s Restaurant" });
await restaurant.save();
res.status(200).json({
"token": token,
"user": user
});

最佳答案

最后两个网络调用是独立的,因此您可以使用promise.all来调用它们,以便调用并行进行,

let restaurant = new Restaurant({ userId: saveUser._id, hotelCode: hotelCode, name: req.body.name.split(/[\s,]+/)[0] + "'s Restaurant" });
await Promise.all[transporter.sendMail(mailOptions), restaurant.save()]

关于node.js - 使用 async wait 后代码非常慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57478598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com