Just reviewing my own code and noticed that I'm saving info in the DB and WAITING for that to respond before I send info to the browser, although I'm wondering if it would be more performant to not wait...
For example, let's update a user's email address.
Currently, I get a user, update their info, save and wait for confirmation, then send the browser the data:
UserModel.findById(user._id)
.then(function(user){
user.email = '[email protected]';
return user.save();
})
.then(function(user){
res.send(user.email)
})
But the user data is already edited, and I already have it, so can't I send the browser the data and save to MongoDB at the same time...?
UserModel.findById(user._id)
.then(function(user){
user.email = '[email protected]';
user.save();
res.send(user.email)
})
I guess, now that I'm typing it out, the only issue (I can imagine) would be if there was an error with the saving operation at the DB level... How often does that even happen? It's really only a matter of milliseconds, but just wondering if anyone has done this or can share a horror store of why I definitely should not do this.
res
? Please clarify. \$\endgroup\$res
seems to be a normal Node.js Response object to me. \$\endgroup\$