Medium
JavaScript: Why is my async discount calculation in shopping cart returning a Promise instead of the total?
You're building a shopping cart feature and need to calculate the total price after applying a discount code. What's wrong with this implementation?
1function calculateDiscountedTotal(cart, discountCode) {2 const subtotal = cart.reduce((sum, item) => sum + item.price, 0);34 const applyDiscount = async () => {5 const response = await fetch(`/api/discounts/${discountCode}`);6 const { percentage } = await response.json();7 return subtotal * (1 - percentage / 100);8 }910 return applyDiscount();11}1213// Usage14const total = calculateDiscountedTotal(cartItems, "SUMMER20");15console.log(`Total after discount: $${total}`);