Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added solution with promises with then and solution with async await #1

Open
wants to merge 1 commit into
base: nicoleta-tuturuga
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions promise-exercise/app.js
Original file line number Diff line number Diff line change
@@ -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);