-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
71 lines (58 loc) · 1.75 KB
/
Copy pathapp.js
File metadata and controls
71 lines (58 loc) · 1.75 KB
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
// ******************************
// Refactoring with Promise.all ******************************
// Example 1
// Sequential Requests
// async function get3Pokemon() {
// const poke1 = await axios.get('https://pokeapi.co/api/v2/pokemon/1')
// const poke2 = await axios.get('https://pokeapi.co/api/v2/pokemon/2')
// const poke3 = await axios.get('https://pokeapi.co/api/v2/pokemon/3')
// console.log(poke1.data)
// console.log(poke2.data)
// console.log(poke3.data)
// }
// get3Pokemon()
// Example 2
// Parallel Requests
async function get3Pokemon() {
const prom1 = axios.get('https://pokeapi.co/api/v2/pokemon/1')
const prom2 = axios.get('https://pokeapi.co/api/v2/pokemon/2')
const prom3 = axios.get('https://pokeapi.co/api/v2/pokemon/3')
const results = await Promise.all([prom1, prom2, prom3])
console.log(results)
printPokemon(results)
}
function printPokemon(results) {
for(pokemon of results) {
console.log(pokemon.data.name);
}
}
get3Pokemon()
// Example 3
// function changeBodyColor(color, delay) {
// return new Promise((resolve, reject) => {
// setTimeout(() => {
// document.body.style.backgroundColor = color
// resolve()
// }, delay)
// })
// }
// Sequential Requests
// async function lightShow() {
// await changeBodyColor('teal', 1000)
// await changeBodyColor('pink', 1000)
// await changeBodyColor('indigo', 1000)
// await changeBodyColor('violet', 1000)
// }
// lightShow()
// Parallel Requests
// async function lightShow() {
// const p1 = changeBodyColor('teal', 1000)
// const p2 = changeBodyColor('pink', 1000)
// const p3 = changeBodyColor('indigo', 1000)
// const p4 = changeBodyColor('violet', 1000)
// await p1
// await p2
// await p3
// await p4
// }
// lightShow()