liveBook · Manning

Promise

As explained in Vanilla Web

A JavaScript promise is an object representing the eventual completion or failure of an asynchronous operation and its resulting value.

A promise represents the eventual completion or failure of an asynchronous operation. With Fetch, it resolves to a Response; an HTTP error status still resolves, so check response.ok or response.status yourself. section 12.1.1

Handle rejections explicitly: attach catch() to a promise chain, or use try/catch around await inside an async function. Unhandled rejections trigger the global unhandledrejection event. section 5.9.3

Load and check JSON

Await fetch(), throw when response.ok is false, then parse the response with response.json(). Catch failures at the call site to avoid unhandled rejections. section 5.9.3 section 12.1.1

Listing 12.1 A reusable helper for JSON requests
async function requestJSON(url, options = {}) {
  const { headers, ...fetchOptions } = options;

  const response = await fetch(url, {
    ...fetchOptions,
    headers: {
      Accept: "application/json",
      ...headers,
    },
  });

  if (!response.ok) {
    throw new Error(`Request failed with ${response.status}`);
  }

  return response.json(); #1
}

See a reusable JSON request helper that sets headers, checks response status, and returns the parsed body. section 12.1.1

Programmatic navigation can return promises for different milestones: committed signals that the URL and current entry changed, while finished signals that navigation work—including an intercepted route handler—has completed. section 11.2

How Vanilla Web covers it

How deep it goes
Explains resolution and rejection through Fetch and navigation examples, including status checks and error handling, but not promise construction internals.
Examples
Uses JSON requests, response checks and parsing, rejection handling, and route-navigation completion.
What it assumes
Assumes familiarity with ES2015 JavaScript and treats async/await as part of baseline modern JavaScript knowledge.
What you can do after
Apply it via Fetch and Navigation APIs to await data or route completion and handle rejected operations.
How it connects
Connects promises with Fetch, async/await, and the Navigation API.

Also explained in

Compare how 11 books explain promise →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Vanilla Web. The section links point to the parts of the book it draws on.