liveBook · Manning

Pending state

As explained in Secrets of the JavaScript Ninja, Second Edition

The promise state in which the final outcome has not yet been determined.

A promise is pending while its asynchronous result is still unknown. This unresolved state lasts until the promise fulfills with a value or rejects with an error. section 6.3.2

A promise leaves the pending state in either of two ways:

  • Calling resolve fulfills the promise with the promised value. section 6.3.2
  • Calling reject, or encountering an unhandled exception during promise handling, rejects it. section 6.3.2
  • Once fulfilled or rejected, a promise cannot switch to the other state. section 6.3.2

Waiting for a timeout

A promise that resolves after a 500-millisecond timeout remains pending while it waits. When the timeout calls resolve with “Hattori,” the promise fulfills. section 6.3.2

Figure 6.10. States of a promise

Use the state diagram to follow a promise from pending to either fulfilled or rejected. section 6.3.2

How Secrets of the JavaScript Ninja, Second Edition covers it

How deep it goes
Defines the state transitions to fulfillment or rejection and notes that settled promises cannot switch states.
Examples
Uses a 500-millisecond timeout to delay resolving a promise with “Hattori.”
What you can do after
Understand how pending differs from fulfilled and rejected promise states.
How it connects
Connects promise state to the resolve and reject functions and to the fulfilled and rejected states.

Also explained in

Compare how 3 books explain pending state →

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.