-
Notifications
You must be signed in to change notification settings - Fork 1
/
IntroToFetch.js
48 lines (39 loc) · 961 Bytes
/
IntroToFetch.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
// fetch('https://swapi.dev/api/people/1').then(response =>{
// // console.log(response)
// return response.json();
// })
// .then(data =>{
// return fetch(data.films[0]);
// })
// .then(response =>{
// return response.json();
// })
// .then(data =>{
// console.log(data);
// })
const printPlanetNames = (data) =>{
console.log(data);
console.log('Printing 10 planet names');
data.results.forEach(planet =>{
console.log(planet.name);
});
return Promise.resolve(data.next);
}
const fetchNextPlanets = url =>{
return fetch(url);
}
fetch('https://swapi.dev/api/planets')
.then(getResponse)
.then(printPlanetNames)
.then(fetchNextPlanets)
.then(getResponse)
.then(printPlanetNames)
.catch(err =>{
console.log('SOMETHING WENT WRONG '+err);
});
function getResponse(response){
if(!response.ok){
throw new Error(`Status code error :${response.status}`);
}
return response.json();
};