-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathes8.js
75 lines (56 loc) · 1.41 KB
/
es8.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
// object spread operator
const animals = {
tiger: 23,
lion: 5,
monkey: 2,
bird:40,
}
function objectSpread(p1, p2, p3) {
console.log(p1);
console.log(p2);
console.log(p3);
}
const { tiger, lion, ...rest } = animals;
objectSpread(tiger, rest);
//// FINALLY
const urls = [
'http://swapi.dev/api/people/1',
'http://swapi.dev/api/people/2',
'http://swapi.dev/api/people/3',
'http://swapi.dev/api/people/4'
]
Promise.all(urls.map(url => {
fetch(url).then(people => people.json())
}))
.then(array => {
console.log("1", array[0])
console.log("2", array[1])
console.log("3", array[2])
console.log("4", array[3])
})
.catch(error => console.log("fuck shit went wrong", error));
.finally(() => console.log('extra'));
// for await of
const urls = [
"https://jsonplaceholder.typicode.com/users",
"https://jsonplaceholder.typicode.com/posts",
"https://jsonplaceholder.typicode.com/albums",
];
const getData = async function () {
const [users, posts, albums] = await Promise.all(
urls.map(async function (url){
const response = await fetch(url);
return response.json();
}),
);
console.log("users", users);
console.log("posta", posts);
console.log("albums", albums);
};
const getDataTwo = async function() {
const arrayOfPromises = urls.map(url => fetch(url));
for await (let request of arrayOfPromises) {
const data = await request.json();
console.log(data);
}
}