const cart = [“shoes”, “pant”, “kurta”];
const orderId = “12345”;
const accountId = “someId001”;
let walletBalance = “1000”;
function createOrder(orderId) {
returnnewPromise((resolve, reject) => {
setTimeout(() => {
console.log(`OrderId : ${orderId} Created`);
resolve(orderId);
}, 5000);
});
}
function proceedToPayment(orderId) {
returnnewPromise((resolve, reject) => {
setTimeout(() => {
console.log(`Payment processed for Order Id: ${orderId}`);
resolve(orderId);
}, 1000);
});
}
function showOrderSummary(orderId) {
returnnewPromise((resolve) => {
console.log(`order summary for ${orderId} is ${cart.join(“, “)}`);
resolve(orderId);
});
}
function updateWallet(userAccountId) {
returnnewPromise((resolve) => {
constcost=800; // just assume that the total cost is 800rs
walletBalance-=cost;
console.log(`total amount left in the wallet is : ${walletBalance} rs`);
resolve(walletBalance);
});
}
createOrder(orderId)
.then((orderId) => proceedToPayment(orderId))
.then((orderId) => showOrderSummary(orderId))
.then(() => updateWallet(accountId))
.catch((err) => {
console.log(err.message);
});
Here is one humble attempt to make the promise chaining in working.
2 Comments
I’m typically to blogging and i actually admire your content. The article has actually peaks my interest. I’m going to bookmark your web site and preserve checking for brand new information.
1