Specifically I'm working with Mongoose and Node, but I guess this is a more conceptual question about asynchronous coding.
I see this example all over Mongoose docs:
product.sold = Date.now();
product.save(function (err, product, numberAffected) {
if (err) ..
})
However, what if
product.save(...)
executes faster than
product.sold = Date.now()
Wouldn't you be saving before updating...? Maybe I'm missing something here (at a conceptual level)? What's keeping this code "safe" in an asynchronous environment.
More specifically I'm using doc.addToSet as the "update" step, and I'd feel much better if it had a callback I could embed the doc.save step in (to ensure async behavior). Thoughts?
PS. I'm not simply using model.update because I need validation.
product.sold = Date.now();