liveBook · Manning

Then method

As explained in Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond

A Promise method that registers callbacks to run when the promise is fulfilled or rejected, returning a new promise for chaining.

Suppose an AJAX request is loading a user’s organizations. How do you use the response without knowing when it will arrive? The request returns a promise, and `.then()` registers a callback to receive its eventual value. If the promise is still pending, the callback waits; if it has already resolved, the callback still runs, but through the event loop rather than immediately. section 30.0 section 30.1

Use a completed request

```js axios.get('https://api.github.com/users/jisaacks/orgs') .then(resp => { const orgs = resp.data; // use the organizations }); ``` First, `axios.get` starts the request and returns a promise. Calling `.then()` supplies the function that will receive the response after the request succeeds. The organizations are read from that response, not from the promise itself. section 30.1

A promise can have `.then()` called on it more than once. That makes it useful when loading starts before the result is needed: a user view can start fetching organizations in its constructor, then call `.then()` when it is time to display them. The display step works whether loading has finished or is still underway. section 30.1 section 30.4

Handle either result

```js axios.get(url).then(handleResp, handleError); ``` The first callback handles a successful response. The optional second callback receives an error when the promise is rejected. For a quick check, take a callback-based `getJSON` call that receives `(err, json)` and rewrite its success and error branches as these two `.then()` callbacks. section 30.2

Each call to `.then()` returns a new promise, so calls can form a chain. `.catch(errCatcher)` is shorthand for `.then(null, errCatcher)`; responses and rejections can move through that chain. For an already available value, `Promise.resolve(value)` provides a resolved promise that can still be used with `.then()`—handy when a function should return a promise for both cached and newly loaded data. section 31.4 section 30.3

How Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond covers it

How deep it goes
Walks through fulfillment timing and an optional rejection callback; defers further error-handling techniques while identifying chaining in the advanced summary.
Examples
Uses an axios GitHub API request, an early-loading user view, a cached transaction, and a callback-to-promise JSON exercise.
What it assumes
Assumes familiarity with JavaScript callbacks and arrow functions while defining a promise as an object representing a future value.
What you can do after
Apply it via axios to consume responses, handle request errors, and use the same callback pattern for pending or resolved promises.
How it connects
Connects promise callbacks to AJAX, callback-based APIs, Promise.resolve, promise chains, and catch.

Also explained in

Compare how 5 books explain then method →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond. The section links point to the parts of the book it draws on.