-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync-challenges.js
143 lines (108 loc) · 3.56 KB
/
async-challenges.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* CHALLENGE 1 */
function sayHowdy() {
console.log('Howdy');
}
function testMe() {
setTimeout(sayHowdy, 0);
console.log('Partnah');
}
// After thinking it through, uncomment the following line to check your guess!
// testMe(); // what order should these log out? Howdy or Partnah first?
/* CHALLENGE 2 */
function delayedGreet() {
setTimeout(() => {console.log('welcome');}, 3000);
}
// Uncomment the following line to check your work!
// delayedGreet(); // should log (after 3 seconds): welcome
/* CHALLENGE 3 */
function helloGoodbye() {
setTimeout(() => {console.log("good bye")}, 2000)
console.log("hello");
}
// Uncomment the following line to check your work!
// helloGoodbye(); // should log: hello // should also log (after 3 seconds): good bye
/* CHALLENGE 4 */
function brokenRecord() {
setInterval(() => {console.log("hi again")}, 1000)
}
// Uncomment the following line to check your work!
// brokenRecord(); // should log (every second): hi again
/* CHALLENGE 5 */
function limitedRepeat() {
let timerId = setInterval(() => console.log("hi for now"), 1000);
setTimeout(() => clearInterval(timerId), 5000);
}
// Uncomment the following line to check your work!
// limitedRepeat(); // should log (every second, for 5 seconds): hi for now
/* CHALLENGE 6 */
function everyXsecsForYsecs(func, int, dur) {
let timerId = setInterval(() => func(), int*1000);
setTimeout(()=> clearInterval(timerId), dur*1000);
}
// Uncomment the following lines to check your work!
// function theEnd() {
// console.log('This is the end!');
// }
// everyXsecsForYsecs(theEnd, 2, 20); // should invoke theEnd function every 2 seconds, for 20 seconds): This is the end!
/* CHALLENGE 7 */
function delayCounter(target, wait) {
return (() => {
let i=0;
let timerId = setInterval(() => { i++; console.log(i)}, wait);
setTimeout(()=> clearInterval(timerId), target*1000);
})
}
// UNCOMMENT THESE TO TEST YOUR WORK!
// const countLogger = delayCounter(3, 1000)
// countLogger();
// After 1 second, log 1
// After 2 seconds, log 2
// After 3 seconds, log 3
/* CHALLENGE 8 */
function promised (val) {
return new Promise((resolve) => {
setTimeout(()=> resolve(val), 2000)
})
}
// UNCOMMENT THESE TO TEST YOUR WORK!
// const createPromise = promised('wait for it...');
// createPromise.then((val) => console.log(val));
// will log "wait for it..." to the console after 2 seconds
/* CHALLENGE 9 */
class SecondClock {
constructor(cb) {
this.cb = cb;
}
start(){ this.timerId = setInterval(()=>{
if(this.cb >= 60) this.cb = 0;
this.cb++;
console.log(this.cb)
}, 1000)}
reset(){this.cb = 0; clearInterval(this.timerId)}
}
// UNCOMMENT THESE TO TEST YOUR WORK!
// const clock = new SecondClock(58);
// console.log("Started Clock.");
// clock.start();
// setTimeout(() => {
// clock.reset();
// console.log("Stopped Clock after 6 seconds.");
// }, 6000);
/* CHALLENGE 10 */
const debounce = (callback, interval) => {
let timerStart = new Date();
return () => {
const invocationTime = new Date();
if (invocationTime - timerStart > interval) {
timerStart = new Date();
return callback();
}
};
};
// UNCOMMENT THESE TO TEST YOUR WORK!
// function giveHi() { return 'hi' }
// const giveHiSometimes = debounce(giveHi, 3000);
// console.log(giveHiSometimes()); // -> 'hi'
// setTimeout(function() { console.log(giveHiSometimes()); }, 2000); // -> undefined
// setTimeout(function() { console.log(giveHiSometimes()); }, 4000); // -> undefined
// setTimeout(function() { console.log(giveHiSometimes()); }, 8000); // -> 'hi'