A colleague want to refactor the backend code of a project into Async/Await, which I barely ever used.
I know how it works, but it's kind of strange to write code this way at the beginning.
router.post("/register", async (req, res) => {
const { firstName, lastName, email, password } = req.body;
bcrypt.hash(password, 10).then((hash) => {
User.create({
firstName: firstName,
lastName: lastName,
email: email,
password: hash,
});
res.json("User created!");
});
});
How would you refactor this simple piece of code, for example? Thanks!!!
const hash = await bcrypt.hash(password, 10);the rest should be self explanatorythenis them moved after that line.