Skip to content

Commit adef9b1

Browse files
committed
Section 17: Chaining Fetch Requests
1 parent c332fca commit adef9b1

4 files changed

Lines changed: 50 additions & 0 deletions

File tree

17-Making-HTTP-Requests/06/app.css

Whitespace-only changes.

17-Making-HTTP-Requests/06/app.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// ******************************
2+
// Chaining Fetch Requests ******************************
3+
// Example 1
4+
5+
console.log('Chaining Fetch Requests')
6+
7+
// Throwing error to the .catch
8+
fetch('https://swapi.dev/api/planets/')
9+
.then((response) => {
10+
if (!response.ok) throw new Error(`Status Code Error: ${response.status}`)
11+
12+
return response.json()
13+
})
14+
.then((data) => {
15+
// console.log(data);
16+
// console.log(data.results[0].films[0])
17+
18+
console.log('FETCHED ALL PLANETS (first 10)')
19+
const filmURL = data.results[0].films[0]
20+
return fetch(filmURL)
21+
})
22+
.then((response) => {
23+
if (!response.ok) throw new Error(`Status Code Error ${response.status}`)
24+
25+
return response.json()
26+
})
27+
.then((data) => {
28+
console.log('FETCHED FIRST FILM, based off of first planet')
29+
// console.log(data);
30+
console.log(data.title)
31+
})
32+
.catch((err) => {
33+
console.log('SOMETHING WHEN WRONG WIT FETCH!')
34+
console.log(err)
35+
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<link rel="stylesheet" href="app.css" />
8+
<title>JavaScript</title>
9+
</head>
10+
<body>
11+
<script src="app.js"></script>
12+
</body>
13+
</html>

17-Making-HTTP-Requests/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,5 @@ fetch('https://icanhazdadjoke.com/23/2', {
169169
```
170170

171171
#### Fetch API MDN: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
172+
173+
## 6. Chaining Fetch Requests

0 commit comments

Comments
 (0)