-
Notifications
You must be signed in to change notification settings - Fork 1
/
promise.js
73 lines (59 loc) · 1.75 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
roomCleaningProbability = 75; // declared in main.js
/* Setting up a promise that can be subsequently used */
function cleanRoom (kid) {
/*
- `new` creates a new object that inherits from the
specified Constructor, in this case Promise
- `resolve` is our success callback
- `reject` is our failure callback
*/
return new Promise( function(resolve, reject) {
/* setTimeout is used to simulate unknown delay */
setTimeout( function() {
const roomWasCleaned = roomCleaningEvent();
console.log(`\n${kid} cleaned his/her room:`, roomWasCleaned);
if (roomWasCleaned) {
resolve('(Success) ' + kid);
}
else {
reject('(Failure) ' + kid);
}
}, lag(minCleanLag, maxCleanLag));
})
}
// /* Using a promise */
// const aliceCleansRoom = cleanRoom('Alice'); // our promise object
// aliceCleansRoom // how we handle our promise's response
// .then(
// // onResolved
// function(result) {
// console.log(result + ', Good job!')
// },
// // onRejected
// function(err) {
// console.log(err + ', You\'re grounded!')
// }
// );
// aliceCleansRoom
// .then(
// rewardIndividual, // onResolved
// punishIndividual // onRejected
// )
// .finally(logInFamilyCalendar);
// const bobbyCleansRoom = cleanRoom('Bobby');
// bobbyCleansRoom
// .then(rewardIndividual) // onResolved
// .catch(punishIndividual) // onRejected
// .finally(logInFamilyCalendar);
// const charlieCleansRoom = cleanRoom('Charlie');
// charlieCleansRoom
// .then(rewardIndividual)
// .catch(punishIndividual)
// .finally(logInFamilyCalendar);
// Promise.all([
// aliceCleansRoom,
// bobbyCleansRoom,
// charlieCleansRoom
// ])
// .then(() => log('\nFAMILY STATUS: Ice Cream Store, here we come!\n\n'))
// .catch(err => log(`\nFAMILY STATUS: ${err}`));