liveBook · Manning

Resolve

As explained in Secrets of the JavaScript Ninja, Third Edition

A resolve function is the function associated with a promise that resolves it with a value or causes it to adopt a thenable's outcome.

A promise’s resolve function is supplied to its executor to control the promise’s fate. Calling resolve(value) fulfills it with an ordinary value; if value is a thenable—an object with a then method—the promise adopts that thenable’s outcome. Calling resolve with no argument fulfills the promise with undefined. A promise can settle only once. section 8.1.1

Listing 8.2 Resolving promises with other promises
const fulfilledPromise = Promise.resolve("fulfilled"); #A
const rejectedPromise = Promise.reject(new Error("rejected")); #A
 
const promise1 = new Promise((resolve) => resolve(fulfilledPromise));
const promise2 = new Promise((resolve) => resolve(rejectedPromise));

promise1.then((value) => { #B
  console.log(`promise1 was fulfilled with value "${value}"`); #B
}); #B
promise2.then(undefined, (reason) => { #C
  console.log(`promise2 was rejected with reason "${reason}"`); #C
}); #C

Resolving with fulfilled or rejected promises shows how resolution adopts the other promise’s outcome. section 8.1.1

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Distinguishes ordinary values from thenables and shows adoption of fulfillment or rejection, without detailing thenable internals.
Examples
Uses omitted arguments yielding undefined and resolution with fulfilled or rejected promises.
What you can do after
Apply it by resolving a promise with a value or another promise to propagate its eventual outcome.
How it connects
Connects promise resolution to executors, thenables, and rejection.

Also explained in

Compare how 2 books explain resolve →

Related concepts

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