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 with a
then
method whose behavior conforms to this specification. - "thenable" is an object or function that defines a
then
method. - "value" is any legal JavaScript value (including
undefined
, a thenable, or a promise). - "exception" is a value that is thrown using the
throw
statement. - "reason" is a value that indicates why a promise was rejected.
A promise must be in one of three states: pending, fulfilled, or rejected.
-
When pending, a promise:
- may transition to either the fulfilled or rejected state.
-
When fulfilled, a promise:
- must not transition to any other state.
- must have a fulfillment value, which must not change.
-
When rejected, a promise:
- must not transition to any other state.
- must have a rejection reason, which must not change.
Here, "must not change" means immutable identity (i.e. ===
), but does not imply deep immutability.
A promise must provide a then
method to access its current or eventual fulfillment value or rejection reason.
A promise's then
method accepts 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
-
then
must return beforeonFulfilled
oronRejected
is called [4.1]. -
onFulfilled
andonRejected
must be called as functions (i.e. with nothis
value). [4.2] -
then
may be called multiple times on the same promise.- If/when
promise
is fulfilled, all respectiveonFulfilled
callbacks must execute in the order of their originating calls tothen
. - If/when
promise
is rejected, all respectiveonRejected
callbacks must execute in the order of their originating calls tothen
.
- If/when
-
then
must return a promise [4.3].promise2 = promise1.then(onFulfilled, onRejected);
- If either
onFulfilled
oronRejected
returns a valuex
, run the Promise Resolution Procedure[[Resolve]](promise2, x)
. - If either
onFulfilled
oronRejected
throws an exceptione
,promise2
must be rejected withe
as the reason. - If
onFulfilled
is not a function andpromise1
is fulfilled,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.
- If either
The promise resolution procedure is an abstract operation taking as input a promise and a value, which we denote as [[Resolve]](promise, x)
. If x
is a thenable, it attempts to make promise
adopt the state of x
, under the assumption that x
behaves at least somewhat like a promise. Otherwise, it fulfills promise
with the value x
.
This treatment of thenables allows promise implementations to interoperate, as long as they expose a Promises/A+-compliant then
method. It also allows Promises/A+ implementations to "assimilate" nonconformant implementations with reasonable then
methods.
To run [[Resolve]](promise, x)
, perform the following steps:
- If
x
is a promise, adopt its state [4.4]:- If
x
is pending,promise
must remain pending untilx
is fulfilled or rejected. - If/when
x
is fulfilled, fulfillpromise
with the same value. - If/when
x
is rejected, rejectpromise
with the same reason.
- If
- Otherwise, if
x
is an object or function,- Let
then
bex.then
. [4.5] - If retrieving the property
x.then
results in a thrown exceptione
, rejectpromise
withe
as the reason. - If
then
is a function, call it withx
asthis
, first argumentresolvePromise
, and second argumentrejectPromise
, where:- If/when
resolvePromise
is called with a valuey
, run[[Resolve]](promise, y)
. - If/when
rejectPromise
is called with a reasonr
, rejectpromise
withr
. - If both
resolvePromise
andrejectPromise
are called, or multiple calls to the same argument are made, the first call takes precedence, and any further calls are ignored. - If calling
then
throws an exceptione
,- If
resolvePromise
orrejectPromise
have been called, ignore it. - Otherwise, reject
promise
withe
as the reason.
- If
- If/when
- If
then
is not a function, fulfillpromise
withx
.
- Let
- If
x
is not an object or function, fulfillpromise
withx
.
-
In practical terms, an implementation must use a mechanism such as
setTimeout
,setImmediate
, orprocess.nextTick
to ensure thatonFulfilled
andonRejected
are not invoked in the same turn of the event loop as the call tothen
to which they are passed. -
That is, in strict mode
this
will beundefined
inside of them; in sloppy mode, it will be the global object. -
Implementations may allow
promise2 === promise1
, provided the implementation meets all requirements. Each implementation should document whether it can producepromise2 === promise1
and under what conditions. -
Generally, it will only be known that
x
is a true promise if it comes from the current implementation. This clause allows the use of implementation-specific means to adopt the state of known-conformant promises. -
This procedure of first storing a reference to
x.then
, then testing that reference, and then calling that reference, avoids multiple accesses to thex.then
property. Such precautions are important for ensuring consistency in the face of an accessor property, whose value could change between retrievals.
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.