-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
189 lines (158 loc) · 4.85 KB
/
demo.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
"use strict";
console.log("Hello there Fullstackers!")
function add(a, b) {
return a + b;
}
const add2 = (a, b) => {
return a + b;
}
console.log("Compare regular vs arrow functions:", add(3, 5), add2(3, 5));
//return;
//hoisted = 5;
//console.log("Let's see what hoisted is:", hoisted);
function varInFunction() {
var foo = 5;
}
varInFunction();
//console.log("What is foo?", foo); // What if you uncomment this?
function varInBlockInFunction() {
var foo = 10;
console.log("What is foo in the beginning?", foo);
if (true) {
var foo = 5;
console.log("What is foo in this block?", foo);
}
// WWJsD
console.log("After the block, what is foo?", foo);
}
varInBlockInFunction();
function letInBlockInFunction() {
let bar = 10;
console.log("What is bar in the beginning?", bar);
if (true) {
let bar = 5;
console.log("What is bar in this block?", bar);
}
// WWJsD
console.log("After the block, what is bar?", bar);
}
letInBlockInFunction();
const me = {
petName: "dog",
numberOfPets: 1,
getPetName: function() {
return this.petName;
},
morePets: function() {
if (this !== undefined) {
this.numberOfPets++;
}
}
};
console.log("If `this` refers to `me`, I should get my pet's name:", me.getPetName());
const stolenMorePets = me.morePets;
stolenMorePets();
stolenMorePets();
// WWJsD
console.log("If `this` refers to `me`, I should have 3 pets:", me.numberOfPets);
const you = {
petName: "cat",
numberOfPets: 1,
defineFnLater: undefined
};
const youCanHaveThem = stolenMorePets.bind(you);
youCanHaveThem();
youCanHaveThem();
// WWJsD
console.log("If `this` refers to `you`, you should get 3 pets:", you.numberOfPets);
you.defineFnLater = me.getPetName;
// WWJsD
console.log("If `this` is re-bound, you should see your pet's name:", you.defineFnLater());
const justALittleSillyThing = {
petName: "hamster",
numberOfPets: 1,
getPetNameButIAmLying: () => {
return this.petName;
}
}
// WWJsD
console.log("If `this` refers to `justALittleSillyThing`, I should get my pet's name:",
justALittleSillyThing.getPetNameButIAmLying());
function callbackHell() {
setTimeout(() => console.log("Late for school"), 1000);
const somethingThatTakesAWhile = (doAfterJobCompletes) => {
setTimeout(doAfterJobCompletes, 1000);
}
somethingThatTakesAWhile(
() => {
console.log("Finished the first job, going onto the second");
somethingThatTakesAWhile(
() => {
console.log("Finished second job, going onto the third");
somethingThatTakesAWhile(
() => console.log("Finished third job") // callback hell
);
}
);
}
);
}
//callbackHell(); // Comment this after
console.log("Why does this text appear so early?");
//setTimeout(() => console.log("Late for school1"), 0);
//setTimeout(() => console.log("Late for school2"), 0);
//setTimeout(() => console.log("Late for school3"), 0);
const betterJob = (willSucceed, time) => new Promise((res, rej) => {
if (willSucceed) {
setTimeout(res, time);
} else {
setTimeout(rej, time);
}
});
function promiseChained() {
const doTheBetterJob = betterJob(true, 1000);
doTheBetterJob
.then(
() => console.log("Successfully did the job"),
() => console.log("Job failed")
).catch(
() => console.log("Wait what?")
);
const doTheBetterJob2 = betterJob(false, 2000);
doTheBetterJob2
.then(
() => console.log("Successfully did the job"),
() => console.log("Job failed")
).catch(
() => console.log("Wait what?")
);
const doTheBetterJob3 = betterJob(true, 3000);
doTheBetterJob3
.then(
() => Promise.reject("Nvm job failed"),
() => console.log("Job failed")
).catch(
(e) => console.log(e)
);
}
//promiseChained(); // Comment this after
async function promiseAsync() {
const actuallyResolvesSomething = (willSucceed, time, val) => new Promise((res, rej) => {
if (willSucceed) {
setTimeout(() => { res(val); }, time);
} else {
setTimeout(() => { rej(val); }, time);
}
});
try {
const betterJob4 = await actuallyResolvesSomething(true, 1000, 5);
console.log("On success, returns 5:", betterJob4);
const betterJob5 = await actuallyResolvesSomething(false, 1000, 10);
console.log("On success, returns 10:", betterJob5);
} catch (e) {
console.log("Uh-oh:", e);
}
console.log("Notice how job 4 and 5 have the same timeout but didn't print at the same time");
}
promiseAsync();
console.log("Does this print last?"); // No