Skip to content

Question 43 / 50

Must KnowMediumDebuggingConcurrency

Two checkouts can both buy the last Mechanical Keyboard. What is wrong, and how do you fix it?

const product = await products.findOne({ _id: productId });
if (product.stock < 1) throw new Error("out of stock");
await products.updateOne(
  { _id: productId },
  { $set: { stock: product.stock - 1 } }
);
Think about it first.