Skip to content

Commit e91216b

Browse files
committed
update to lesson 3
1 parent 9b8ba0a commit e91216b

7 files changed

Lines changed: 84 additions & 8 deletions

File tree

‎lessons/02-promises/lecture/NOTES.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ It would be good to mention that most the time we use promises, we're consumers
9696
// 6.
9797

9898
// Change the lesson to start
99-
import fetch from 'node-fetch'
99+
// import fetch from 'node-fetch'
100100

101101
function getVehicle(url: string) {
102102
return fetch(url).then((response) => response.json())
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Request Response Lecture
2+
3+
Start by getting the attendees familiar with the basics of
4+
5+
```js
6+
new Request('url')
7+
new Response()
8+
```
9+
10+
These are the foundations of what `fetch` operates on.
11+
12+
```js
13+
// These are the same:
14+
fetch('site.com')
15+
16+
const req = new Request('site.com')
17+
fetch(req)
18+
```
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// import { fakeFetch } from './utils'
2+
3+
/****************************************
4+
Part One: Request
5+
*****************************************/
6+
7+
function getPersonVehicles(id: number) {
8+
fetch(`https://swapi.dev/api/people/${id}`).then(() => {
9+
console.log('Promise is resolved')
10+
})
11+
}
12+
13+
getPersonVehicles(1)
14+
15+
/****************************************
16+
Part Two: Response
17+
*****************************************/
18+
19+
// Docs: "[fetch] resolves to the Response object representing the response to your request."
20+
// https://developer.mozilla.org/en-US/docs/Web/API/fetch#return_value
21+
22+
// function getPersonVehicles(id: number) {
23+
// fetch(`https://swapi.dev/api/people/${id}`).then((stuff) => {
24+
// console.log('What is this', stuff)
25+
// })
26+
// }
27+
28+
// getPersonVehicles(1)
29+
30+
/****************************************
31+
Custom Fetch
32+
*****************************************/
33+
34+
// fakeFetch('fake-api')
35+
// .then((res) => res.json())
36+
// .then((data) => {
37+
// console.log(data)
38+
// })
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export function fakeFetch(path: string) {
2+
const responseData = { hello: 'world' }
3+
const res = new Response(JSON.stringify(responseData), {
4+
status: 200,
5+
headers: {
6+
'Content-Type': 'application/json; charset=utf-8',
7+
},
8+
})
9+
return Promise.resolve(res)
10+
}
11+
12+
// Note that Response.json() is not the same thing as response.json()
13+
// The one we're showing here is the static method to do all the same
14+
// stuff as we did above:
15+
16+
// https://developer.mozilla.org/en-US/docs/Web/API/Response/json_static
17+
18+
// export function fakeFetch(path: string) {
19+
// return Promise.resolve(Response.json({ hello: 'world' }))
20+
// }

‎lessons/04-async-await/practice/index.final.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import fetch from 'node-fetch'
1+
// import fetch from 'node-fetch'
22

3-
// Each exercise below implements a main() function
3+
// Each practice below implements a main() function
44
main()
55

66
/****************************************
7-
Exercise: 1
7+
Practice: 1
88
*****************************************/
99

1010
function wait(ms: number) {
@@ -21,7 +21,7 @@ async function main() {
2121
}
2222

2323
/****************************************
24-
Exercise: 2
24+
Practice: 2
2525
*****************************************/
2626

2727
// Re-write all functions to use async/await
@@ -51,7 +51,7 @@ async function main() {
5151
// }
5252

5353
/****************************************
54-
Exercise: 3
54+
Practice: 3
5555
*****************************************/
5656

5757
// Your starting point here is actually the solution for the above exercise

‎lessons/04-async-await/practice/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fetch from 'node-fetch'
1+
// import fetch from 'node-fetch'
22

33
// Each practice below implements a main() function
44
main()

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"/////////// LECTURES:": "",
99
"lecture-1": "node lessons/01-js-and-node/lecture",
1010
"lecture-2": "ts-node lessons/02-promises/lecture/index.ts",
11-
"lecture-3": "",
11+
"lecture-3": "ts-node lessons/03-request-response/lecture/index.ts",
1212
"lecture-4": "ts-node lessons/04-async-await/lecture/index.ts",
1313
"lecture-5": "ts-node lessons/05-cli/lecture/index.ts",
1414
"lecture-6": "nodemon --exec \"ts-node lessons/06-server/lecture/index.ts\" || exit 0",

0 commit comments

Comments
 (0)