Skip to content

Chapter 19 of 33

Browser Storage and Fetch

Use local/session storage, HTTP requests, responses, cancellation, and safe persistence.

44 minutes 10 quick checksBy Subha Prasad
Lesson 19 of 33Course navigation

Lesson content

Read, practise, then check your understanding

localStorage persists origin-scoped strings; sessionStorage lasts for one origin/tab session. Both are synchronous, capacity-limited, and unsuitable for secrets. IndexedDB fits larger structured data.

Fetch with boundaries

const controller = new AbortController();
const response = await fetch("/api/lessons", {
  signal: controller.signal,
  headers: { Accept: "application/json" }
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const data = await response.json();

Fetch rejects for network failure, not ordinary HTTP error status. Validate response type/data, define timeouts/cancellation, and encode requests correctly. Storage events can notify other tabs. Treat all client storage as user-controlled and avoid blocking large serialization on the main thread.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes localStorage?
Which JavaScript term matches this description: Origin-scoped synchronous string storage persisting across sessions.
Which statement best describes sessionStorage?
Which JavaScript term matches this description: Origin-and-tab-scoped string storage lasting for the page session.
Which statement best describes fetch?
Which JavaScript term matches this description: A Promise-based API for HTTP request and response handling.
Which statement best describes Response.ok?
Which JavaScript term matches this description: A boolean indicating an HTTP status in the successful 200-299 range.
Which statement best describes AbortController?
Which JavaScript term matches this description: A controller whose signal can cancel supported asynchronous operations.

0 of 10 checks passed

Your progress is saved on this device.