-
Notifications
You must be signed in to change notification settings - Fork 129
/
Callbacks and Promises - 2.js
executable file
·325 lines (263 loc) · 7.18 KB
/
Callbacks and Promises - 2.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* Callbacks and Promises
* Part 1
*
*/
// Callback:
/*
- A callback function is a function that is passed to another function
as a parameter.
- This inner function is called at some point during the execution
of the containing function
- In other words, it's "called back" at some specified point
inside the containing function's body
*/
function shouldGoFirst(callBack) {
setTimeout(() => {
console.log("I should always go first");
callBack();
}, 1000);
}
function shouldGoSecond() {
console.log("I should always go second");
}
function sumUpNumbers(num1, num2, cb) {
let summedValue;
setTimeout(() => {
summedValue = num1 + num2;
cb(summedValue);
}, 1000)
}
function logSummedValue(val) {
console.log(`The summed total is: ${val}`);
}
// sumUpNumbers(100, 150, logSummedValue);
// Callback function
function sayWhenDone(loopCount) {
console.log(`Done! :D. Capitalized ${loopCount} names`);
}
// Parent function
function looper(arr, cb) {
let i = 0;
for(i ; i < arr.length ; i += 1) {
const name = arr[i];
const capitalizedName = name.charAt(0).toUpperCase() + name.slice(1);
arr[i] = capitalizedName;
}
cb(i);
}
const myNames = ["chris", "russell", "toby", "angela"];
// looper(myNames, sayWhenDone);
function anotherLogger(num1, num2, somethingElse) {
const squaredAndSummedNums = (num1 * num1) + (num2 * num2);
console.log(squaredAndSummedNums);
if (somethingElse) {
somethingElse(squaredAndSummedNums);
}
}
// anotherLogger(10, 50, function(p) {
// console.log(`Squared and summed = ${p}`);
// });
const myDiv = document.getElementById("main");
const myButton = myDiv.querySelector("button");
const fakeData = {
text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus eu nulla sit amet ex eleifend finibus vitae et arcu. Nullam maximus nulla sit amet elementum ullamcorper. Mauris tristique massa sit amet urna imperdiet, vel sagittis felis facilisis. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nullam vel augue quis ante varius cursus quis in arcu. Morbi ut pellentesque lorem, non malesuada lacus. Interdum et malesuada fames ac ante ipsum primis in faucibus.",
}
const myPara = document.getElementById("content");
myButton.addEventListener("click", function() {
requestData(populateDOM);
});
function requestData(cb) {
let data = "Loading..."
cb(data);
// Fake server request and response
setTimeout(() => {
// Response from server:
data = fakeData.text;
cb(data);
}, 2000);
}
function populateDOM(data) {
myPara.innerText = data;
}
function counter() {
setTimeout(() => {
console.log("First");
setTimeout(() => {
console.log("Second");
setTimeout(() => {
console.log("Third");
setTimeout(() => {
console.log("Fourth");
}, 400);
}, 600);
}, 800);
}, 1000);
}
// counter();
function numCruncher1(num, cb) {
const newNum = num * num;
cb(newNum);
}
function numCruncher2(num, cb) {
const anotherNewNum = num / 100;
cb(anotherNewNum);
}
function totalSum(a, b, cb) {
cb(a + b);
}
function crunchNumbers(a, b, cb1, cb2, cb3) {
cb1(a, function(x) {
cb2(b, function(y) {
cb3(x, y, function(result) {
console.log(result);
});
});
});
}
// crunchNumbers(5, 10, numCruncher1, numCruncher2, totalSum);
// Promises
/*
- "A Promise is a proxy for a value not necessarily known
when the promise is created" - Mozilla Developer Network
- Promises (similar to callbacks) are used for async computations
- Think of a promise as representing as a value that may be available
now, later, or never
- Can associate a handler with an async action
- A promise exists in three states:
- Pending: initial state, not fulfilled
- Fulfilled: OK! Got it
- Rejected: failed
*/
const testPromise = new Promise((resolve, reject) => {
if (Math.random() > 0.5) {
reject("promise no good! Rejected");
}
setTimeout(() => {
resolve("promise OK!");
}, 1000);
});
// testPromise.then((resolveMessage) => {
// console.log(`Looks like: ${resolveMessage}`);
// }).then(() => {
// console.log("I should run after the promise is resolved");
// }).catch((rejectMessage) => {
// console.log(`Error: ${rejectMessage}`);
// });
function numAdder(n1, n2) {
return new Promise((resolve, reject) => {
const addedNums = n1 + n2;
setTimeout(() => {
resolve(addedNums);
}, 500);
});
}
function numSquarer(num) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(num * num);
}, 800);
});
}
// numAdder(10, 10)
// .then(data => numSquarer(data))
// .then(moreData => console.log(moreData))
// .catch(err => console.log(err));
function alwaysResolves() {
return Promise.resolve("I love resolving :D");
}
alwaysResolves().then(data => console.log(data))
const prom = Promise.resolve([10, 20, 30]);
prom
.then(nums => nums.map(num => num * 10))
.then(transformedNums => console.log(transformedNums));
const anotherProm = Promise.resolve({text: "resolved :D"});
anotherProm.then(data => console.log(data.text))
Promise.resolve()
.then(
res => {
return Promise.reject();
},
err => {
console.log("rejected");
}
).then(
res => {
console.log("cool")
},
err => {
console.log("Inner promise rejected");
}
).catch(data => console.log(data));
function timeLogger(message, time) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(message);
}, time);
if (typeof message !== "string" || typeof time !== "number") {
reject();
}
});
}
timeLogger("first", 1000)
.then(message => {
console.log(message)
return timeLogger("second", 800);
}).then(message => {
console.log(message);
return timeLogger("third", 100);
}).then(message => {
console.log(message);
return timeLogger("fourth", 50);
}).then(message => {
console.log(message);
}).catch(err => console.log("incorrect input"));
Promise.resolve()
.then(() => {
setTimeout(() => {
console.log("first");
return Promise.resolve();
}, 1000);
}).then(() => {
setTimeout(() => {
console.log("second")
}, 500);
})
// return in then
// effectively same as saying return Promise.resolve(...)
Promise.resolve("Hi")
.then(string => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(string + " there, ");
}, 100);
});
}).then(string => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(string + " Chris");
}, 1);
});
})
.then(string => {
console.log(string);
});
const userName = new Promise((resolve, reject) => {
setTimeout(() => {
reject({text: "error"});
}, 1000);
});
const position = new Promise((resolve, reject) => {
setTimeout(() => {
resolve({text: "manager"});
}, 300);
});
// const employees = {
// }
Promise.all([userName, position])
.then(data => data.map(entry => entry.text))
.then(content => employees[0] = content)
.catch(err => console.log(err));
Promise.race([userName, position])
.then(data => console.log(data.text))
.catch(err => console.log(err.text))