JavaScript Curriculum
Fetch API
mediumThe coffee shop menu is now managed in a CMS. Instead of hardcoding cards in HTML, JavaScript fetches the menu from an API, builds the DOM, and keeps everything in sync — no page reload needed.
Fetch API
fetch() returns a Promise. Combined with async/await, it lets you load data from any server without page reloads. Every modern web app is built on this foundation.
GET, POST, PUT, DELETE
The four HTTP methods — each with its own purpose and request shape:
Read data from an API. No body. Parameters go in the URL as query string.
// Simple GET
const res = await fetch('https://api.thegrind.com/menu')
const data = await res.json()
console.log(data.items) // → [{id:1, name:'Espresso'...}]
// GET with query params:
const params = new URLSearchParams({ category: 'coffee', sort: 'price' })
const res2 = await fetch(`/api/menu?${params}`)Promise States — Visualized
Watch what happens to a Promise through its lifecycle:
async function loadMenu() {
const res = await fetch('/api/menu')
if (!res.ok) throw new Error(`HTTP ${res.status}`)
const data = await res.json()
renderCards(data.items)
}
loadMenu()// Click to simulate a fetch()
Callbacks → .then() → async/await
The evolution of async patterns — and why async/await wins:
async/await makes async code look and behave like synchronous code. Errors use familiar try/catch. This is the modern standard — use it for all new code.
// ✓ async/await — reads like sync code
async function loadMenu() {
try {
const res = await fetch('/api/menu')
if (!res.ok) throw new Error(`HTTP ${res.status}`)
const data = await res.json()
const filtered = await filterItems(data.items)
renderCards(filtered)
} catch (err) {
console.error('Failed:', err)
showError()
}
}
loadMenu()Your Challenge
Write loadMenu() using async/await. Fetch /api/menu, check res.ok, parse JSON, render cards with createCard(). Wrap in try/catch — show .error-banner on failure. Then write loadAll() using Promise.all to fetch menu and specials in parallel.
Challenge
Write an async loadMenu() function that fetches '/api/menu', checks res.ok, parses the JSON, and renders cards. Add a try/catch that shows an error banner if the fetch fails. Then write a parallel Promise.all that fetches both the menu and the day's specials simultaneously.
Fetch API
mediumThe coffee shop menu is now managed in a CMS. Instead of hardcoding cards in HTML, JavaScript fetches the menu from an API, builds the DOM, and keeps everything in sync — no page reload needed.
Fetch API
fetch() returns a Promise. Combined with async/await, it lets you load data from any server without page reloads. Every modern web app is built on this foundation.
GET, POST, PUT, DELETE
The four HTTP methods — each with its own purpose and request shape:
Read data from an API. No body. Parameters go in the URL as query string.
// Simple GET
const res = await fetch('https://api.thegrind.com/menu')
const data = await res.json()
console.log(data.items) // → [{id:1, name:'Espresso'...}]
// GET with query params:
const params = new URLSearchParams({ category: 'coffee', sort: 'price' })
const res2 = await fetch(`/api/menu?${params}`)Promise States — Visualized
Watch what happens to a Promise through its lifecycle:
async function loadMenu() {
const res = await fetch('/api/menu')
if (!res.ok) throw new Error(`HTTP ${res.status}`)
const data = await res.json()
renderCards(data.items)
}
loadMenu()// Click to simulate a fetch()
Callbacks → .then() → async/await
The evolution of async patterns — and why async/await wins:
async/await makes async code look and behave like synchronous code. Errors use familiar try/catch. This is the modern standard — use it for all new code.
// ✓ async/await — reads like sync code
async function loadMenu() {
try {
const res = await fetch('/api/menu')
if (!res.ok) throw new Error(`HTTP ${res.status}`)
const data = await res.json()
const filtered = await filterItems(data.items)
renderCards(filtered)
} catch (err) {
console.error('Failed:', err)
showError()
}
}
loadMenu()Your Challenge
Write loadMenu() using async/await. Fetch /api/menu, check res.ok, parse JSON, render cards with createCard(). Wrap in try/catch — show .error-banner on failure. Then write loadAll() using Promise.all to fetch menu and specials in parallel.
Challenge
Write an async loadMenu() function that fetches '/api/menu', checks res.ok, parses the JSON, and renders cards. Add a try/catch that shows an error banner if the fetch fails. Then write a parallel Promise.all that fetches both the menu and the day's specials simultaneously.