How to handle async concurrent requests correctly?
Let's say I have some sort of game. I have a buyItem function like this: buyItem: function (req, res) { // query the users balance // deduct user balance // buy the item } If I spam that route until the user balance is deducted (the 2nd query) the user's balance is still positive. What I have tried: buyItem: function (req, res) { if(req.session.user.busy) return false; req.session.user.busy = true; // query the users balance // deduct user balance // buy the item } The problem is req.session.user.busy will be undefined for the first ~5 requests. So that doesn't work either. How do we handle