liveBook · Manning

Rejected state

As explained in Secrets of the JavaScript Ninja, Second Edition

The promise state indicating unsuccessful completion with a rejection reason.

A promise enters the rejected state when an asynchronous task fails: the promised value was not obtained, but the reason for failure is known. section 6.3.2

A promise becomes rejected when:

  • Its reject function is called. section 6.3.2
  • An unhandled exception occurs during promise handling. section 6.3.2
  • It reaches this state, it cannot switch to fulfilled or another state. section 6.3.2

Choosing the failure callback

In the simple Hattori promise, calling reject(...) instead of resolve(...) sends handling to the second callback passed to then, the failure callback. section 6.3

Figure 6.10. States of a promise

Use the promise-state diagram to compare rejection with pending and fulfilled states. section 6.3.2

How Secrets of the JavaScript Ninja, Second Edition covers it

How deep it goes
Defines rejection, explains its triggers, and notes that the state cannot change once reached.
Examples
Uses a Hattori promise with success and failure callbacks, plus an alternate reject call.
What you can do after
Understand when a promise rejects and how its failure callback differs from its success callback.
How it connects
Connects rejection to Promise, pending and fulfilled states, the reject function, and unhandled exceptions.

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.