This proposal clarifies the behavioral clauses of the Promises/A proposal, extending it to cover de facto behaviors and omitting parts that are underspecified or problematic.
As with Promises/A, this proposal does not deal with how to create, fulfill, or reject promises.
For a full description of the differences between Promises/A+ and Promises/A, see Differences from Promises/A.
A promise represents a value that may not be available yet. The primary method for interacting with a promise is its then
method.
- "promise" is an object or function that defines a
then
method. - "value" is any legal JS value, including
undefined
, that is not a promise. - "reason" is a value that indicates why a promise was rejected.
- "must not change" means immutable identity (i.e.
===
), but does not imply deep immutability.
- A promise must be in one of three states: pending, fulfilled, or rejected.
- When in pending:
- a promise may transition to either the fulfilled or rejected state.
- if the promise transitions to fulfilled, it must provide you a way to call a function with the promise's fulfillment value.
- if the promise transitions to rejected, it must provide you a way to call a function with the promise's reason value.
- When in fulfilled:
- a promise must not transition to any other state.
- a promise must have a value, which must not change.
- a promise must provide you a way to call a function with the promise's fulfillment value.
- When in rejected:
- must not transition to any other state.
- a promise must have a reason, which must not change.
- a promise must provide you a way to call a function with the promise's reason value.
A promise's then
method accepts the following two arguments:
promise.then(onFulfilled, onRejected)
- Both
onFulfilled
andonRejected
are optional arguments:- If
onFulfilled
is not a function, it must be ignored. - If
onRejected
is not a function, it must be ignored.
- If
- If
onFulfilled
is a function:- it must be called after
promise
is fulfilled, withpromise
's fulfillment value as its first argument. - it must not be called more than once.
- it must not be called if
onRejected
has been called.
- it must be called after
- If
onRejected
is a function,- it must be called after
promise
is rejected, withpromise
's rejection reason as its first argument. - it must not be called more than once.
- it must not be called if
onFulfilled
has been called.
- it must be called after
onFulfilled
andonRejected
must not be called beforethen
returns [1].- Calling
then
must return a promise [2] - You may call
then
multiple times.- If you supply
onFulfilled
oronRejected
to a call tothen
, these callbacks must execute before anyonFulfilled
oronRejected
functions supplied to subsequentthen
calls.
- If you supply
var promise2 = promise1.then(onFulfilled, onRejected);
- If either
onFulfilled
oronRejected
returns a value,promise2
must be fulfilled with that value. - If either
onFulfilled
oronRejected
throws an exception,promise2
must be rejected with the thrown exception as the reason. - If either
onFulfilled
oronRejected
returns a promise (call itreturnedPromise
),promise2
must be placed into the same state asreturnedPromise
:- If
returnedPromise
is pending,promise2
must remain pending untilreturnedPromise
is fulfilled or rejected. - If
returnedPromise
is fulfilled,promise2
must be fulfilled with the same value. - If
returnedPromise
is rejected,promise2
must be rejected with the same reason.
- If
- If
onFulfilled
is not a function andpromise1
is fufilled,promise2
must be fulfilled with the same value. - If
onRejected
is not a function andpromise1
is rejected,promise2
must be rejected with the same reason.
-
In practical terms, an implementation must use a mechanism such as
setTimeout
, or faster alternatives such assetImmediate
orprocess.nextTick
, to ensure that the JS engine does not invokeonFulfilled
andonRejected
in the same turn of the event loop as their parentthen
call. -
Implementations are free to allow
promise2 === promise1
, provided the implementation meets all requirements. Each implementation should document whether it can producepromise2 === promise1
and under what conditions.
To the extent possible under law,
the Promises/A+ organization
has waived all copyright and related or neighboring rights to
Promises/A+ Promise Specification.
This work is published from:
United States.