liveBook · Manning

Promise.reject

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

A Promise constructor method that returns a promise already rejected with the given reason.

`Promise.reject(reason)` returns a promise that is already rejected with `reason`. Use it when a rejected promise is needed; a rejection handler passed as the second callback to `.then` receives that reason. section 30.3 section 30.4

Promise.reject('my value').then(null, reason => {
  console.log('Rejected with:', reason);               1
});
!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":1,\"ch\":55},{\"line\":1,\"ch\":56}]]"}
!@%STYLE%@!

This example rejects with `'my value'`, then passes the rejection reason to `.then`’s rejection callback. section 30.3

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

How deep it goes
Defines the pre-rejected result and demonstrates handling its reason without explaining implementation internals.
Examples
Shows `Promise.reject('my value')` passing its reason to a `.then` rejection callback.
What it assumes
Uses promises and `.then` callbacks; does not explain promise basics from zero.
What you can do after
Apply it via the Promise API to deliver a rejection reason to a `.then` rejection handler.
How it connects
Connects promise rejection to `.then` rejection callbacks and contrasts with `Promise.resolve`.

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.