Add lesson 7: fetch and promises

Daniel Tanaka 1 year ago
parent 0bf32b471c commit 6075bc66dcf08ab046548d417c0a2c235f0f2ade
3 changed files with 31 additions and 0 deletions
+19-0lessons/07-fetch-and-promises/README.md ADDED
@@ -0,0 +1,19 @@
1+# Lesson 7: fetch and promises
2+
3+We look at fetch and promises the way working developers use it, with examples you can change and run right away.
4+
5+Full lesson: [https://glibcode.net/courses/javascript-web/lessons/7](https://glibcode.net/courses/javascript-web/lessons/7)
6+
7+## Files
8+
9+- `fetch.js`: fetch with then() and error handling
10+
11+## Run it
12+
13+```sh
14+node fetch.js
15+```
16+
17+## Exercises
18+
19+See [exercises.md](exercises.md). Ask in the forum once you've tried.
+5-0lessons/07-fetch-and-promises/exercises.md ADDED
@@ -0,0 +1,5 @@
1+# Exercises: lesson 7
2+
3+1. Make the example ask for its input instead of using a fixed value.
4+2. Add a check that prints a warning when a result looks wrong (negative, empty, too big).
5+3. Combine this lesson with the previous one: use something from both in a small program of your own.
+7-0lessons/07-fetch-and-promises/fetch.js ADDED
@@ -0,0 +1,7 @@
1+fetch("https://api.github.com/repos/torvalds/linux")
2+ .then(res => {
3+ if (!res.ok) throw new Error(`HTTP ${res.status}`);
4+ return res.json();
5+ })
6+ .then(repo => console.log(`${repo.full_name}: ${repo.stargazers_count} stars`))
7+ .catch(err => console.error("Request failed:", err.message));