-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4.promise example.js
More file actions
46 lines (38 loc) · 794 Bytes
/
Copy path4.promise example.js
File metadata and controls
46 lines (38 loc) · 794 Bytes
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
const cart= ["bag","makup kit","bag"];
const promise= createOrder(cart);
promise
.then(function(orderId)
{
document.write("Order has been created for user id: "+orderId);
///proceedToPayment(orderId);
})
.catch((err)=>
{
document.write(err);
})
function createOrder(cart)
{
const pr = new Promise((resolve,reject)=>
{
//validate cart
//return orderId
if(!validateCart(cart))
{
const err = new Error("Cart is not validate");
reject(err);
}
else{
const orderId=Math.floor(Math.random() * 1000);
//false timing to show async operation
setTimeout(()=>
{
resolve(orderId);
},5000)
}
});
return pr;
}
function validateCart(cart)
{
return true;
}