-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromises.js
99 lines (88 loc) · 2.7 KB
/
promises.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
setTimeout(() => {
const startingWindowWidth = window.innerWidth;
const scrollToBottom = () => {
document.documentElement.scrollTop = document.body.clientHeight;
}
document.body.style.background = 'orange';
document.body.textContent = `Ok lets keep some promises guys.
Can you:
- Wait 20 seconds?
- Click on the lower-right quadrant of the screen?
- Resize the friggin window to half its current width?
`;
//
// WAIT PROMISE
//
const waitPromise = new Promise((resolve) => {
setTimeout(() => {
resolve('\n\ndamn u are patient hoss, you waited 20 whole seconds');
}, 20000);
});
//
// AFTER WAIT PROMISE IS RESOLVED
//
Promise.resolve(waitPromise).then((response) => {
document.body.style.background = 'yellow';
document.body.textContent += response;
scrollToBottom();
});
//
// CLICK PROMISE
//
const clickPromise = new Promise((resolve) => {
const handleClick = (event) => {
const clickX = event.clientX;
const clickY = event.clientY;
const isInLowerRightQuadrant = clickX > window.innerWidth / 2 && clickY > window.innerHeight / 2;
if (isInLowerRightQuadrant) {
window.removeEventListener('click', handleClick);
resolve('\n\nThank you for clicking CORRECTLY');
} else {
document.body.textContent += '\n\nYou are CLICKING WRONG!!';
scrollToBottom();
}
};
window.addEventListener('click', handleClick);
});
//
// AFTER CLICK PROMISE IS RESOLVED
//
Promise.resolve(clickPromise).then((response) => {
document.body.style.background = 'green';
document.body.textContent += response;
scrollToBottom();
});
//
// RESIZE PROMISE
//
const resizePromise = new Promise((resolve) => {
const handleResize = (event) => {
window.requestAnimationFrame(() => {
if (window.innerWidth <= startingWindowWidth / 2) {
window.removeEventListener('resize', handleResize);
resolve('\n\nResizing the browser window is one of your talents');
} else {
document.body.textContent += '\n' + window.innerWidth;
scrollToBottom();
}
});
};
window.addEventListener('resize', handleResize);
});
//
// AFTER RESIZE PROMISE IS RESOLVED
//
Promise.resolve(resizePromise).then((response) => {
document.body.style.background = 'pink';
document.body.textContent += response;
scrollToBottom();
});
//
// AFTER ALL PROMISES HAVE BEEN RESOLVED
//
Promise.all([waitPromise, clickPromise, resizePromise]).then((responses) => {
document.body.textContent += '\n\nAll promises have been resolved!';
scrollToBottom();
console.log('these were their responses:', responses);
});
}, 3000);