-
Notifications
You must be signed in to change notification settings - Fork 77
/
assignment3Solution.js
90 lines (74 loc) · 1.94 KB
/
assignment3Solution.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
/**
* Q: Write Code that
*
* 1. logs hi after 1 second
* 2. logs hello 3 seconds after step 1
* 3. logs hello there 5 seconds after step 2
*/
/*
// callback hell example - nested callbacks
setTimeout(function () {
// print hi after 1 second
console.log("hi");
// print hello after 3 seconds
setTimeout(function () {
console.log("hello");
// print hello there after 5 seconds
setTimeout(function () {
console.log("hello there");
}, 5000);
}, 3000);
}, 1000);
*/
// callback hell example - nested callbacks
// function to print hello there
function step3Done() {
console.log("hello there");
}
// function to print hello after 3 seconds and call step3Done after 5 seconds
function step2Done() {
console.log("hello");
// call step3Done after 5 seconds
setTimeout(step3Done, 5000);
}
// function to print hi after 1 second and call step2Done after 3 seconds
function step1Done() {
console.log("hi");
// call step2Done after 3 seconds
setTimeout(step2Done, 3000);
}
// call step1Done after 1 second
setTimeout(step1Done, 1000);
/*
// Promisified Version
function setTimeoutPromisified(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
setTimeoutPromisified(1000).then(function () {
console.log("hi");
setTimeoutPromisified(3000).then(function () {
console.log("hello");
setTimeoutPromisified(5000).then(function () {
console.log("hello there");
});
});
});
*/
/*
// Promisified Version
function setTimeoutPromisified(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
setTimeoutPromisified(1000)
.then(function () {
console.log("hi");
return setTimeoutPromisified(3000);
})
.then(function () {
console.log("hello");
return setTimeoutPromisified(5000);
})
.then(function () {
console.log("hello there");
});
*/