Guest User

Untitled

a guest
Dec 15th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. function submitOrder(user) {
  2. var shoppingCart, zipCode, shippingRate, orderSuccessful;
  3.  
  4. // Get the current user's shopping cart
  5. OrderAPI.getShoppingCartAsync(user)
  6. .then(function(cart) {
  7. shoppingCart = cart;
  8. return CustomerAPI.getProfileAsync(user);
  9. })
  10. .then(function(profile) {
  11. // Also look up the ZIP code from their profile
  12. zipCode = profile.zipCode;
  13. })
  14. .then(function(x) {
  15. // Calculate the shipping fees
  16. shippingRate = calculateShipping(shoppingCart, zipCode);
  17. return OrderAPI.placeOrderAsync(shoppingCart, shippingRate);
  18. })
  19. .then(function(success) {
  20. orderSuccessful = success;
  21. });
  22.  
  23. // Submit the order
  24.  
  25. console.log(`Your order ${orderSuccessful? "was" : "was NOT"} placed successfully`);
  26. }
  27.  
  28. //The same thing with async await (someone else's solution)
  29. async function submitOrder2(user) {
  30. let shoppingCart, zipCode, shippingRate, orderSuccessful;
  31.  
  32. // Get the current user's shopping cart
  33. shoppingCart = await OrderAPI.getShoppingCartAsync(user);
  34.  
  35. // Also look up the ZIP code from their profile
  36. zipCode = (await CustomerAPI.getProfileAsync(user)).zipCode;
  37.  
  38. // Calculate the shipping fees
  39. shippingRate = calculateShipping(shoppingCart, zipCode);
  40.  
  41. // Submit the order
  42. orderSuccessful = await OrderAPI.placeOrderAsync(shoppingCart, shippingRate);
  43. console.log(`Your order ${orderSuccessful? "was" : "was NOT"} placed successfully`);
  44. }
Add Comment
Please, Sign In to add comment