Skip to content

Instantly share code, notes, and snippets.

@asynchroza
Created May 26, 2026 08:15
Show Gist options
  • Select an option

  • Save asynchroza/544e6e78f6e7eb8add6b8196379da696 to your computer and use it in GitHub Desktop.

Select an option

Save asynchroza/544e6e78f6e7eb8add6b8196379da696 to your computer and use it in GitHub Desktop.
Interview Task
async function checkout(userId, items) {
const order = await db.orders.create({
userId,
status: "pending",
createdAt: Date.now()
});
items.forEach(async item => {
const inventory = await db.inventory.get(item.productId);
if (inventory.stock < item.qty) {
throw new Error("Out of stock");
}
await db.inventory.update(item.productId, {
stock: inventory.stock - item.qty
});
});
const paymentPromise = paymentGateway.charge(userId, calculateTotal(items));
analytics.track("checkout_started", {
userId,
orderId: order.id
});
paymentPromise.then(result => {
db.orders.update(order.id, {
paymentId: result.id,
status: "paid"
});
email.sendReceipt(userId, order.id);
analytics.track("checkout_success", {
orderId: order.id
});
});
return order;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment