Implementers provide open standards for implementers to implement reliable, interoperable JavaScript promises.
Promise represents the end result of an asynchronous operation. The primary way to interact with a promise is through its then method, which registers callbacks to receive the final value of a promise or the reason why a promise cannot be fulfilled.
The specification details the behavior of the THEN method and provides an interoperable foundation that can be relied upon by all Promises/A+ compliant promise implementations. Therefore, the specification should be considered very stable. Although Promises/A+ organizations may occasionally modify this specification and make minor backward compatible changes to address newly discovered extremes, we will only integrate large or backward incompatible changes after careful consideration, discussion, and testing.
Historically, Promises/A+ clarifies the act clause of early Promises/A proposals by expanding it to include actual acts and omits unspecified or problematic parts.
Finally, the core Promises/A+ specification does not deal with how to create, implement, or reject Promises, choosing instead to focus on providing interoperable THEN methods. Future work in supporting specifications may address these topics.
1. The term
- 1.1. “Promise” is
then
Method that behaves in accordance with this specification. - 1.2. “Thenable” is the definition
then
The object or function of a method. (Promise is the Thenable object) - 1.3. “value” is any valid JavaScript value (including
undefined
, thenable or promise). - 1.4. “exception” means use
throw
The value thrown by the statement. - 1.5. “Reason” is the reason why the promise failed.
2. Request
2.1. Promise
Promise must be in one of three states: Pending, depressing, or Rejected.
2.1.1. In pending state, Promise:
- 2.1.1.1. This can be a pity or rejected.
This is a big pity. I Promise:
- 2.1.2.1. There shall be no transition to any state.
- 2.1.2.2. There must be a value that cannot be changed.
2.1.3. Rejected state, I Promise:
- 2.1.3.1. There shall be no transition to any state.
- 2.1.3.2. There must be a reason and it cannot be changed.
Here, “must not change” means immutable identity (i.e. ===), but it does not mean deep immutability. (Reference type address unchanged)
2.2.then
methods
A promise must provide a then method to access its current or final value or Reason. Promise’s then method accepts two arguments:
promise.then(onFulfilled, onRejected)
Copy the code
2.2.1. These twoonFulfilled
andonRejected
Optional parameters:
- 2.2.1.1. If
onFulfilled
Is not a function, it must be ignored. - 2.2.1.2. If
onRejected
Is not a function, it must be ignored.
2.2.2. IfonFulfilled
Is a function:
- 2.2.2.1. It must be in
promise
When done call,promise
Value as the first argument. - 2.2.2.2.
promise
It should not be called until it is done. - 2.2.2.3. It cannot be called more than once.
2.2.3. IfonRejected
Is a function:
- 2.2.3.1. It must be in
promise
Is called after being rejected,promise
Reason as the first argument. - 2.2.3.2.
promise
It must not be called until rejected. - 2.2.3.3. It cannot be called more than once.
2.2.4. onFulfilled
oronRejected
inExecution contextThe stack cannot be called until it contains only platform code. (Chain-called, the THEN method returns a new Promise object and needs to wait until the new Promise object is instantiated before calling the then callback.) 3.1 .
2.2.5. onFulfilled
andonRejected
Must be called as a function (i.ethis
Value). 3.2
2.2.6. then
Can be in the samepromise
Called multiple times.
- 2.2.6.1. If/when
promise
This is a depressing state, then all the correspondingonFulfilled
Callbacks must be executed in the original orderthen
. - 2.2.6.2. If/When the Promise is in the Rejected state, then
onRejected
Callbacks must be executed in the original orderthen
.
2.2.7. then
Must return onepromise
3.3 .
promise2 = promise1.then(onFulfilled, onRejected);
Copy the code
- 2.2.7.1. If either
onFulfilled
oronRejected
Return a valuex
(x
May bepromise
, so you need to add a handler), runPromise
To solve the program[[Resolve]](promise2, x)
. - If 2.2.7.2.
onFulfilled
oronRejected
An exception is throwne
That will bee
As apromise2
The rejected reason. - If 2.2.7.3.
onFulfilled
It’s not a function andpromise1
If yes, thenpromise2
Must be used withpromise1
The same value will be fulfilled. - If 2.2.7.4.
onRejected
It’s not a functionpromise1
Is rejected,promise2
Must be connected topromise1
Use the same reason as rejected.
2.3.Promise Solution
The Promise resolver is a function that takes a promise and a value, expressed as [[Resolve]](Promise,x). If x is a Thenable (promise), it will attempt the promise to adopt x’s state, provided that X behaves at least somewhat like a promise. Otherwise, it is returned as the fulfilled value of the promise.
This handling of Thenables allows promise implementations to interoperate as long as they expose the Promise/A+ compatible THEN methods. It also allows Promises/A+ implementations to “assimilate” inconsistent implementations using sound THEN methods.
To run [[Resolve]](promise, x), perform the following steps:
2.3.1. Ifpromise
andx
To reference the same object, use thepromise TypeError
For reason come rejected.
2.3.2. Ifx
ispromise
, takes its state 3.4 :
- 2.3.2.1. If
x
Pending, thenpromise
Must remain pending untilx
This is a pity or rejected. - 2.3.1.2. If/If
x
Fulfilled,promise
Use the same value depressing. - 2.3.1.3. If/If
x
Rejected, pleasepromise
I have no choice but to do it.
2.3.3. Otherwise, ifx
Is an object or function:
- 2.3.3.1. Let
then = x.then
. 3.5 - 2.3.3.2. If the attributes are retrieved
x.then
The result of an exception thrown bye
That will bee
As apromise
The reason for this is rejected. - 2.3.3.3. If
then
Theta is a function of thetax
As athis
Call it, first argumentresolvePromise
And the second parameterrejectPromise
, including:- If 2.3.3.3.1.
resolvePromise
Is called and returns a valuey
(the same principlex
Is likely topromise
), runs[[Resolve]](promise, y)
. - 2.3.3.3.2. If/If
rejectPromise
Is called with a reasonr
That will ber
As refused topromise
“Reason. - 2.3.3.3.3. If these two
resolvePromise
andrejectPromise
If the same parameter is called, or if the same parameter is called several times, the first call takes precedence and any further calls are ignored. - 2.3.3.3.4. If called
then
An exception is throwne
:- If 2.3.3.3.4.1.
resolvePromise
orrejectPromise
Has been called, please ignore it. - 2.3.3.3.4.2. Otherwise
e
As apromise
I have no refute.
- If 2.3.3.3.4.1.
- If 2.3.3.3.1.
- 2.3.3.4. If
then
It’s not a functionx
As apromise
Fulfilled the value.
2.3.4. Ifx
Not an object or function that willx
As apromise
This is very depressing.
If the promise is resolved with thenable participating in the loop mutable chain,the recursive properties that make [[Resolve]](promise,thenable) eventually cause [[Resolve]](promise,thenable) to be called again, following the algorithm above will result in infinite recursion. Implementations are encouraged, but not required, to detect this recursion and reject the promise for providing the message TypeError as a reason. 3.6
3. Notes
-
3.1. “Platform code” here means the engine, environment, and Promise implementation code. In practice, this requires ensuring that the onFulfilled and onRejected execute the THEN asynchronously after calling the event loop and using the new stack. This can be done using “macro task” mechanisms, such as setTimeout or setImmediate, or “microtask” mechanisms, such as MutationObserver or process.nexttick. Because the Promise implementation is considered platform code, it may itself contain a task scheduling queue or “trampoline” that invokes handlers.
-
3.2. In other words, the strict mode this will undefined; In sloppy mode, it becomes a global object.
-
An implementation is allowed as long as it meets all requirements. Each implementation should document whether it can produce promisE2 === promisE1 and under what conditions.
-
3.4. In general, x is only a true promise if it comes from the current implementation. This clause allows implementation-specific methods to adopt the state of a promise that is known to conform.
-
3.5. This procedure first stores the x.teng reference, then tests the reference, and then calls the reference to avoid accessing the X.teng property multiple times. These precautions are important to ensure consistency of visitor attributes, whose values may change between retrievals.
-
3.6. The implementation should not set any limits on the depth of the Thenable chain and assume that beyond any limits the recursion will be infinite. Only true cycles can cause TypeError; Always recursion is the right thing to do if you encounter an infinite number of different possible chains.