-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromisesDataFetching.js
40 lines (34 loc) · 1.14 KB
/
promisesDataFetching.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
import { fakeFetch } from './util/fakeFetch.js';
// if YouTube faster: A, C, B, D
// if Google faster: A, C, D, B
let youtubeResponseGlobal;
let googleResponseGlobal;
function logValues() {
if (youtubeResponseGlobal && googleResponseGlobal) {
console.log(youtubeResponseGlobal);
console.log(googleResponseGlobal);
}
}
// A) Start with YouTube promise
fakeFetch('https://example.com/youtube')
.then((youtubeResponse) => {
// B) Later: finish with YouTube
youtubeResponseGlobal = youtubeResponse;
logValues(); // In both functions, log ALL values (only if they exist)
})
.catch((error) => console.error(error.message));
// C) Start with Google promise
fakeFetch('https://example.com/google')
.then((googleResponse) => {
// D) Later: finish with Google
googleResponseGlobal = googleResponse;
logValues(); // In both functions, log ALL values (only if they exist)
})
.catch((error) => console.error(error.message));
// Promise.all() version - use this in your code
// console.log(
// await Promise.all([
// fakeFetch('https://example.com/youtube'),
// fakeFetch('https://example.com/google'),
// ]),
// );