-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.js
44 lines (39 loc) · 1.11 KB
/
promise.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const cart = ["shoes", "pants", "kurta"];
const promise = createOder(cart);
promise
.then((orderId) => {
console.log(orderId); // if any errors occurs here directly catch block will be execute it skip the praymentInfo To overcome this behavaiour we have add catch block to this then method now if any occurs catch will execute and promise chain will not skip
return orderId;
})
.catch((err) => {
console.log(err);
})
.then((orderId) => proceedToPayment(orderId))
.then((paymentInfo) => console.log(paymentInfo))
.catch((err) => {
console.log(err);
})
.then(() =>
console.log("no matter what happens it will definetely execute or called")
);
function createOder(cart) {
const pr = new Promise((resolve, reject) => {
if (!validateCart(cart)) {
const err = new Error("Cart is not valid");
reject(err);
}
const orderId = "1234";
if (orderId) {
resolve(orderId);
}
});
return pr;
}
function validateCart(cart) {
return true;
}
function proceedToPayment(orderId) {
return new Promise((resolve, reject) => {
resolve("Payment Sucessful");
});
}