-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpromises.html
More file actions
executable file
·84 lines (67 loc) · 2.09 KB
/
Copy pathpromises.html
File metadata and controls
executable file
·84 lines (67 loc) · 2.09 KB
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
81
82
83
84
<!DOCTYPE html>
<html>
<head>
<title>promises.html</title>
</head>
<body>
<script>
'use strict';
/*
function getProductDetails() {
setTimeout(function () {
console.log('Getting customers');
setTimeout(function () {
console.log('Getting orders');
setTimeout(function () {
console.log('Getting products');
setTimeout(function () {
console.log('Getting product details')
}, 1000);
}, 1000);
}, 1000);
}, 1000);
};
getProductDetails();
*/
function getCustomers(){
let promise = new Promise(
function (resolve, reject){
console.log("Getting customers");
// Emulate an async server call here
setTimeout(function(){
let success = true;
if (success){
resolve( "John Smith"); // got the customer
}else{
reject("Can't get customers");
}
},1000);
}
);
return promise;
}
function getOrders(customer){
let promise = new Promise(
function (resolve, reject){
// Emulate an async server call here
setTimeout(function(){
let success = true;
if (success){
resolve( `Found the order 123 for ${customer}`); // got the order
}else{
reject("Can't get orders");
}
},1000);
}
);
return promise;
}
getCustomers()
.then((cust) => {console.log(cust);return cust;})
.then((cust) => getOrders(cust))
.then((order) => console.log(order))
.catch((err) => console.error(err));
console.log("Chained getCustomers and getOrders. Waiting for results");
</script>
</body>
</html>