-
Notifications
You must be signed in to change notification settings - Fork 0
/
exerciseAsyncAwait.js
44 lines (34 loc) · 1.24 KB
/
exerciseAsyncAwait.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
// Solve the below problems:
// #1) Convert the below promise into async await
fetch('https://swapi.co/api/starships/9/')
.then(response => response.json())
.then(console.log)
async function fetchResp(){
const response = await fetch('https://swapi.co/api/starships/9/')
const data = await response.json();
console.log(data);
}
// #2) ADVANCED: Update the function below from the video to also have
// async await for this line: fetch(url).then(resp => resp.json())
// So there shouldn't be any .then() calls anymore!
// Don't get discouraged... this is a really tough one...
const urls = [
'https://jsonplaceholder.typicode.com/users',
'https://jsonplaceholder.typicode.com/posts',
'https://jsonplaceholder.typicode.com/albums'
]
const getData = async function() {
try{
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('posts', posts);
console.log('albums', albums);
} catch (err){
console.log('oops', err)
}};
// #3) Add a try catch block to the #2 solution in order to catch any errors.
// Now chnage one of the urls so you console.log your error with 'ooooooops'