forked from CodesmithLLC/cs-bin-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
async.js
90 lines (69 loc) · 2.23 KB
/
async.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
/* 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?
// 'Partnah' will log out first.
/* CHALLENGE 2 */
function delayedGreet() {
// ADD CODE HERE
}
// Uncomment the following line to check your work!
// delayedGreet(); // should log (after 3 seconds): welcome
/* CHALLENGE 3 */
function helloGoodbye() {
console.log('hello');
setTimeout(() => console.log('good bye'), 2000);
}
// 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() {
const intervalId = setInterval(() => console.log('hi for now'), 1000);
setTimeout(() => clearInterval(intervalId), 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, interval, duration) {
const intervalId = setInterval(func, interval * 1000);
setTimeout(() => clearInterval(intervalId), duration * 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) {
let intervalId;
let counter = 0;
return function inner() {
if (counter === 0) {
counter++;
intervalId = setInterval(() => console.log(inner()), wait);
} else if (counter === target) {
clearInterval(intervalId);
return counter;
} else {
return counter++;
}
}
}
// 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