An open standard for sound, interoperable JavaScript promises—by implementers, for implementers.
A promise represents the eventual result of an asynchronous operation. The primary way of interacting with a promise is through its then method, which registers callbacks to receive either a promise's eventual value or the reason why the promise cannot be fulfilled.
This specification details the behavior of the then method, providing an interoperable base which all Promises/A+ conformant promise implementations can be depended on to provide. As such, the specification should be considered very stable. Although the Promises/A+ organization may occasionally revise this specification with minor backward-compatible changes to address newly-discovered corner cases, we will integrate large or backward-incompatible changes only after careful consideration, discussion, and testing.
Historically, Promises/A+ clarifies the behavioral clauses of the earlier Promises/A proposal, extending it to cover de facto behaviors and omitting parts that are underspecified or problematic.
Finally, the core Promises/A+ specification does not deal with how to create, fulfill, or reject promises, choosing instead to focus on providing an interoperable then method. Future work in companion specifications may touch on these subjects.
- "promise" is an object or function with a
thenmethod whose behavior conforms to this specification. - "thenable" is an object or function that defines a
thenmethod. - "value" is any legal JavaScript value (including
undefined, a thenable, or a promise). - "exception" is a value that is thrown using the
throwstatement. - "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 value, which must not change.
- When rejected, a promise:
- must not transition to any other state.
- must have a 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 value or reason.
A promise's then method accepts two arguments:
promise.then(onFulfilled, onRejected)-
Both
onFulfilledandonRejectedare optional arguments:- If
onFulfilledis not a function, it must be ignored. - If
onRejectedis not a function, it must be ignored.
- If
-
If
onFulfilledis a function:- it must be called after
promiseis fulfilled, withpromise's value as its first argument. - it must not be called before
promiseis fulfilled. - it must not be called more than once.
- it must be called after
-
If
onRejectedis a function,- it must be called after
promiseis rejected, withpromise's reason as its first argument. - it must not be called before
promiseis rejected. - it must not be called more than once.
- it must be called after
-
onFulfilledoronRejectedmust not be called until the execution context stack contains only platform code. [3.1]. -
onFulfilledandonRejectedmust be called as functions (i.e. with nothisvalue). [3.2] -
thenmay be called multiple times on the same promise.- If/when
promiseis fulfilled, all respectiveonFulfilledcallbacks must execute in the order of their originating calls tothen. - If/when
promiseis rejected, all respectiveonRejectedcallbacks must execute in the order of their originating calls tothen.
- If/when
-
thenmust return a promise [3.3].promise2 = promise1.then(onFulfilled, onRejected);
- If either
onFulfilledoronRejectedreturns a valuex, run the Promise Resolution Procedure[[Resolve]](promise2, x). - If either
onFulfilledoronRejectedthrows an exceptione,promise2must be rejected witheas the reason. - If
onFulfilledis not a function andpromise1is fulfilled,promise2must be fulfilled with the same value aspromise1. - If
onRejectedis not a function andpromise1is rejected,promise2must be rejected with the same reason aspromise1.
- 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
promiseandxrefer to the same object, rejectpromisewith aTypeErroras the reason. - If
xis a promise, adopt its state [3.4]:- If
xis pending,promisemust remain pending untilxis fulfilled or rejected. - If/when
xis fulfilled, fulfillpromisewith the same value. - If/when
xis rejected, rejectpromisewith the same reason.
- If
- Otherwise, if
xis an object or function,- Let
thenbex.then. [3.5] - If retrieving the property
x.thenresults in a thrown exceptione, rejectpromisewitheas the reason. - If
thenis a function, call it withxasthis, first argumentresolvePromise, and second argumentrejectPromise, where:- If/when
resolvePromiseis called with a valuey, run[[Resolve]](promise, y). - If/when
rejectPromiseis called with a reasonr, rejectpromisewithr. - If both
resolvePromiseandrejectPromiseare called, or multiple calls to the same argument are made, the first call takes precedence, and any further calls are ignored. - If calling
thenthrows an exceptione,- If
resolvePromiseorrejectPromisehave been called, ignore it. - Otherwise, reject
promisewitheas the reason.
- If
- If/when
- If
thenis not a function, fulfillpromisewithx.
- Let
- If
xis not an object or function, fulfillpromisewithx.
If a promise is resolved with a thenable that participates in a circular thenable chain, such that the recursive nature of [[Resolve]](promise, thenable) eventually causes [[Resolve]](promise, thenable) to be called again, following the above algorithm will lead to infinite recursion. Implementations are encouraged, but not required, to detect such recursion and reject promise with an informative TypeError as the reason. [3.6]
-
Here "platform code" means engine, environment, and promise implementation code. In practice, this requirement ensures that
onFulfilledandonRejectedexecute asynchronously, after the event loop turn in whichthenis called, and with a fresh stack. This can be implemented with either a "macro-task" mechanism such assetTimeoutorsetImmediate, or with a "micro-task" mechanism such asMutationObserverorprocess.nextTick. Since the promise implementation is considered platform code, it may itself contain a task-scheduling queue or "trampoline" in which the handlers are called. -
That is, in strict mode
thiswill beundefinedinside 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 === promise1and under what conditions. -
Generally, it will only be known that
xis 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.thenproperty. Such precautions are important for ensuring consistency in the face of an accessor property, whose value could change between retrievals. -
Implementations should not set arbitrary limits on the depth of thenable chains, and assume that beyond that arbitrary limit the recursion will be infinite. Only true cycles should lead to a
TypeError; if an infinite chain of distinct thenables is encountered, recursing forever is the correct behavior.

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.