From 6b6b8aba3b42424cb8c444991b56c9992529e6af Mon Sep 17 00:00:00 2001 From: nicoleta-tuturuga Date: Sun, 12 Apr 2020 21:24:26 +0300 Subject: [PATCH] added solution with promises with then and solution with async await --- promise-exercise/app.js | 46 +++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/promise-exercise/app.js b/promise-exercise/app.js index 2d17b60..67e6c19 100644 --- a/promise-exercise/app.js +++ b/promise-exercise/app.js @@ -1,30 +1,46 @@ const orderApi = require('./lib/order.api'); const customerApi = require('./lib/customer.api'); + +// ------------------------- +// Using promises with then +// ------------------------- + const submitOrder = (user) => { let shoppingCart, zipCode, shippingRate, orderSuccessful; - // Get the current user's shopping cart orderApi.getShoppingCartAsync(user).then((cart) => { shoppingCart = cart; - }); - - // Also look up the ZIP code from their profile - customerApi.getProfileAsync(user).then((profile) => { + return customerApi.getProfileAsync(user); + }).then((profile) => { zipCode = profile.zipCode; - }); + shippingRate = orderApi.calculateShippingSync(shoppingCart, zipCode); + return orderApi.placeOrderAsync(shoppingCart, shippingRate); + }).then((success) => { + orderSuccessful = success; + console.log(`Your order ${orderSuccessful ? "was" : "was NOT"} placed successfully`); + }) +} + +submitOrder(12345); - // Calculate the shipping fees - shippingRate = orderApi.calculateShippingSync(shoppingCart, zipCode); - // Submit the order - orderApi.placeOrderAsync(shoppingCart, shippingRate).then((success) => { - orderSuccessful = success; - }); +// ------------------------- +// Using aync await +// ------------------------- - console.log(`Your order ${orderSuccessful? "was" : "was NOT"} placed successfully`); -}; +// async function submitOrder(user) { + +// let shoppingCart = await orderApi.getShoppingCartAsync(user); +// let userProfile = await customerApi.getProfileAsync(user); +// let zipCode = userProfile.zipCode; +// let shippingRate = orderApi.calculateShippingSync(shoppingCart, zipCode); +// let orderSuccessful = await orderApi.placeOrderAsync(shoppingCart, shippingRate); +// console.log(`Your order ${orderSuccessful ? "was" : "was NOT"} placed successfully`); +// } + +// submitOrder(12345); -submitOrder(12345); + \ No newline at end of file