liveBook · Manning

Promise

As explained in Secrets of the JavaScript Ninja, Second Edition

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

Fetching data from a server takes time. A synchronous request can leave the browser UI unresponsive; callbacks avoid that pause but make dependent requests and error handling awkward. A promise gives an asynchronous result a single object to work with: the value may arrive later, or the operation may fail. section 6.1 section 6.3

Create one with `new Promise(executor)`. The executor runs immediately and receives `resolve` and `reject` functions. Call `resolve(value)` when the work succeeds or `reject(reason)` when it fails. Register a success callback with `then`; supply a second callback or chain `catch` to handle rejection. section 6.3 section 6.3.3

A result that arrives later

`const result = new Promise((resolve, reject) => { setTimeout(() => resolve("Ready"), 500); }); result.then(value => console.log(value)).catch(error => console.log(error));` The executor sets up the timer immediately. The promise remains pending until the timer resolves it; the registered success callback handles the resulting value. section 6.3.2 section 6.3.3

A promise starts pending. It becomes fulfilled with a value or rejected with a reason, then stays in that state. Calling `reject` is not the only route to failure: an unhandled exception in the executor also rejects the promise. Both kinds of failure reach a rejection handler. section 6.3.2 section 6.3.3

A practical `getJSON(url)` can return a promise backed by `XMLHttpRequest`. Resolve it after a successful response has been parsed as JSON. Reject it for a communication error, an unsuccessful response status, or invalid JSON. The caller can then attach `then` and `catch` without managing those event handlers itself. section 6.3.4

For dependent requests, chain promises with `then`; for parallel asynchronous steps, use `Promise.all`. Promises can also work with generators: yield a promise while the generator pauses, then resume it when the result arrives. This keeps the browser from blocking while making a sequence of requests easier to follow. section 6.4 section 6.5

How Secrets of the JavaScript Ninja, Second Edition covers it

How deep it goes
Works through construction, state transitions, rejection, and a promise-returning JSON request, then extends to chains and generator coordination.
Examples
Uses a delayed timer, explicit and exception-driven rejection, and an XMLHttpRequest-based JSON fetch.
What it assumes
Assumes familiarity with JavaScript functions and callbacks while introducing the Promise constructor and its executor from first principles.
What you can do after
Build it from scratch: wrap an XMLHttpRequest in a promise and handle successful responses, request failures, and invalid JSON.
How it connects
Connects promises to callbacks, XMLHttpRequest, JSON parsing, generators, and Promise.all.

Also explained in

Compare how 7 books explain promise →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Second Edition. The section links point to the parts of the book it draws on.