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” isthenMethod that behaves in accordance with this specification.
  • 1.2. “Thenable” is the definitionthenThe object or function of a method. (Promise is the Thenable object)
  • 1.3. “value” is any valid JavaScript value (includingundefined, thenable or promise).
  • 1.4. “exception” means usethrowThe 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.thenmethods

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 twoonFulfilledandonRejectedOptional parameters:
  • 2.2.1.1. IfonFulfilledIs not a function, it must be ignored.
  • 2.2.1.2. IfonRejectedIs not a function, it must be ignored.
2.2.2. IfonFulfilledIs a function:
  • 2.2.2.1. It must be inpromiseWhen done call,promiseValue as the first argument.
  • 2.2.2.2.promiseIt should not be called until it is done.
  • 2.2.2.3. It cannot be called more than once.
2.2.3. IfonRejectedIs a function:
  • 2.2.3.1. It must be inpromiseIs called after being rejected,promiseReason as the first argument.
  • 2.2.3.2. promiseIt must not be called until rejected.
  • 2.2.3.3. It cannot be called more than once.
2.2.4. onFulfilledoronRejectedinExecution 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. onFulfilledandonRejectedMust be called as a function (i.ethisValue). 3.2
2.2.6. thenCan be in the samepromiseCalled multiple times.
  • 2.2.6.1. If/whenpromiseThis is a depressing state, then all the correspondingonFulfilledCallbacks must be executed in the original orderthen.
  • 2.2.6.2. If/When the Promise is in the Rejected state, thenonRejectedCallbacks must be executed in the original orderthen.
2.2.7. thenMust return onepromise 3.3 .
promise2 = promise1.then(onFulfilled, onRejected);
Copy the code
  • 2.2.7.1. If eitheronFulfilledoronRejectedReturn a valuex(xMay bepromise, so you need to add a handler), runPromiseTo solve the program[[Resolve]](promise2, x).
  • If 2.2.7.2.onFulfilledoronRejectedAn exception is throwneThat will beeAs apromise2The rejected reason.
  • If 2.2.7.3.onFulfilledIt’s not a function andpromise1If yes, thenpromise2Must be used withpromise1The same value will be fulfilled.
  • If 2.2.7.4.onRejectedIt’s not a functionpromise1Is rejected,promise2Must be connected topromise1Use 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. IfpromiseandxTo reference the same object, use thepromise TypeErrorFor reason come rejected.
2.3.2. Ifxispromise, takes its state 3.4 :
  • 2.3.2.1. IfxPending, thenpromiseMust remain pending untilxThis is a pity or rejected.
  • 2.3.1.2. If/IfxFulfilled,promiseUse the same value depressing.
  • 2.3.1.3. If/IfxRejected, pleasepromiseI have no choice but to do it.
2.3.3. Otherwise, ifxIs an object or function:
  • 2.3.3.1. Letthen = x.then. 3.5
  • 2.3.3.2. If the attributes are retrievedx.thenThe result of an exception thrown byeThat will beeAs apromiseThe reason for this is rejected.
  • 2.3.3.3. IfthenTheta is a function of thetaxAs athisCall it, first argumentresolvePromiseAnd the second parameterrejectPromise, including:
    • If 2.3.3.3.1.resolvePromiseIs called and returns a valuey(the same principlexIs likely topromise), runs[[Resolve]](promise, y).
    • 2.3.3.3.2. If/IfrejectPromiseIs called with a reasonrThat will berAs refused topromise“Reason.
    • 2.3.3.3.3. If these tworesolvePromiseandrejectPromiseIf 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 calledthenAn exception is throwne:
      • If 2.3.3.3.4.1.resolvePromiseorrejectPromiseHas been called, please ignore it.
      • 2.3.3.3.4.2. OtherwiseeAs apromiseI have no refute.
  • 2.3.3.4. IfthenIt’s not a functionxAs apromiseFulfilled the value.
2.3.4. IfxNot an object or function that willxAs apromiseThis 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.