-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.js
80 lines (60 loc) · 1.61 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//promises
const cart = ["shoes" , "shirt" , "jenas"];
const promise = createOrder(cart); // no need to assign into varibale we can also use
// createOrder(cart)
// .then
//consumer
promise.then(function(orderid){
console.log(orderid);
return orderid;
})
// handling the error using catch
.catch(function(er){ //it will handle spefic block of error
console.log(er.message);
})
.then(function(orderid){
return proceedToPayment(orderid); //if payement is sucessful then return payment info
})
// handling the error using catch
.catch(function(er){
console.log(er.message);
})
.then(function(payementInfo){
console.log(payementInfo);
})
.catch(function(er){
console.log(er.message);
})
.then(function(orderid){
console.log("No matter what happens , I will be definietely be called.");
})
//producer
function createOrder(cart){
const pr = new Promise(function(resolve , reject){
//createOrder
//validateOrder
//orderid
//if cart is not valid then return error
if(!validateOrder(cart)){
const err = new Error("cart is not valid");
reject(err);
}
//if cart is valid then return cart id
const orderid = 12345;
if(orderid){
setTimeout(function(){
resolve(orderid);
},5000);
}
})
return pr;
}
// letting cart is valid or not valid
function validateOrder(cart){
return true;
}
function proceedToPayment(orderid){
return new Promise(function(resolve,reject){
resolve("Payment Succesful");
})
}