Introduction to the

Promise represents the end result of an asynchronous operation. The primary way to interact with a promise is through the then method, which registers callbacks to receive a promise’s completion value or a reason why a promise could not be fulfilled.

The specification details the behavior of the THEN method and provides an interactive foundation on which all promise implementations that conform to the Promises/A+ specification can rely. Therefore, the specification should be considered very stable. Although Promises/A+ organizations will sometimes revise this specification with 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 earlier Promises/A proposals, expanding it to cover actual acts and omits unspecified or problematic parts.

Finally, the core Promises/A+ specification does not deal with how to create, fulfill, or reject Promises. Instead, it chooses to focus on providing interactive THEN methods. Future regulatory work may address these topics.

1. The term

  • 1.1. A “promise” is an object or function that behaves according to this specification and has then methods.

  • 1.2. “thenable” is an object or function that has then methods.

  • 1.3. “value” is any valid JavaScript value (including undefined, thenable, or promise).

  • 1.4. “exception” is a value thrown using the throw statement.

  • 1.5. “Reason” is used as a reason for a promise rejection.

2. Request

2.1 Promise state

A promise must be in one of three states: Pending, depressing, or Rejected.

2.1.1 When pending, a Promise:

  • 2.1.1.1 There may be a pity or rejected state.

2.1.2 When this is a big pity, a promise:

  • 2.1.2.1 Cannot be changed to any other state.

  • 2.1.2.2 There must be a value which must never be changed.

2.1.3 When in the rejected, I promise:

  • 2.1.3.1 Cannot be changed to any other state.

  • 2.1.3.2 There must be a reason and the reason must never be changed.

In this context, “must never change” means the same itself (i.e. ===), but it does not mean the same depth.

2.2 then method

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 Ondepressing and onRejected are both optional parameters:

  • 2.2.1.1 If ondepressing 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 If ondepressing is a function:

  • 2.2.2.1 A promise must be called after completion, with the promise’s value as its first argument.

  • 2.2.2.2 A promise cannot be invoked before it completes.

  • 2.2.2.3 It cannot be called more than once.

2.2.3 If onRejected is a function:

  • 2.2.3.1 A Promise must be called after it has been rejected, with the Promise’s reson as its first argument.

  • 2.2.3.2 A promise cannot be called before it is rejected.

  • 2.2.3.3 It cannot be called more than once.

2.2.4 inExecution context stackOnFulfilled or onRejected must not be called until only platform code is included. [3.1]

2.2.5 Ondepressing and onRejected must be called as functions, that is, the default call mode. (E.g., no this). [3.2]

2.2.6 The then function may be called multiple times on the same promise.

2.2.6.1 If/when the Promise completes, all corresponding Ondepressing callbacks must be executed in the order in which they were originally called to THEN (i.e. in the order in which they were registered through the THEN method).

2.2.6.2 If/when a Promise rejects, all corresponding onRejected callbacks must be executed in the order in which they were originally called to THEN (that is, in the order in which they were registered through the THEN method).

2.2.7 The then method must return a Promise [3.3].

promise2 = promise1.then(onFulfilled, onRejected);
Copy the code

2.2.7.1 If onFulfilled or onRejected returns the value X, then the Promise Resolution Procedure will be implemented.[[Resolve]](promise2, x).

2.2.7.2 If Ondepressing or onRejected throws exception E, e must be reason to reject promise2.

2.2.7.3 If ondepressing is not a function and promise1 is completed, the same value as promise1 must be used to complete promise2.

2.2.7.4 If onRejected is not a function and promise1 is rejected, the same reason as promisE1 must be used to reject promise1.

2.3 Promise Resolution Procedure

The Promise resolution program is an abstract operation that takes a Promise and value as input, which we express as [[Resolve]](Promise, x). If X is a tenable and behaves like a promise, it tries to make the promise adopt the state of X. Otherwise, it completes the promise with an x value as a value.

This handling of Thenables allows Promises to be interactive, as long as they expose then methods that are compatible with Promises/A+. It also allows Promises/A+ implementations to “absorb” non-compliant implementations with sensible THEN methods.

To run [[Resolve]](promise, x), perform the following steps:

2.3.1 If a Promise and X point to the same object, TypeError is used as a reason to reject the Promise.

2.3.2 If X is a Promise, use its state [3.4] :

  • 2.3.2.1 If x is pending, the promise must wait until x completes or is rejected.

  • 2.3.2.2 If/when X completes, complete the promise with the same value.

  • 2.3.2.3 If/when X rejects, reject the promise with the same reason.

2.3.3 Otherwise, if x is an object or function:

  • 2.3.3.1 Let then be x. teng. [3.5]

  • 2.3.3.2 If the retrieval of attribute X. teng causes exception E to be thrown, the promise is rejected on the grounds of E.

  • 2.3.3.3 If THEN is a function, call it with x with the first argument resolvePromise and the second argument rejectPromise, where:

    • 2.3.3.3.1 If/when resolvePromise is called with the value y, run [[Resolve]](promise, y).

    • 2.3.3.3.2 If/When rejectPromise is invoked with a reason r, reject the promise with r as reason.

    • 2.3.3.3.3 If both resolvePromise and rejectPromise are called, or the same parameter is called more than once, the first call takes precedence and subsequent calls are ignored.

    • 2.3.3.3.4 If calling THEN raises exception E:

      • 2.3.3.3.4.1. If resolvePromise or rejectPromise is called, ignore it.

      • 2.3.3.3.4.2. Otherwise, reject the promise with eas reason.

  • 2.3.3.4 If then is not a function, then x completes the promise.

2.3.4 If x is not an object or function, use X to complete the promise.

If a promise is resolved by thenable in the loop thenable chain, Then the recursive nature of [[Resolve]](promise, thenable) will eventually cause [[Resolve]](promise, thenable) to be called again, following the algorithm will result in infinite recursion. Implementations are encouraged (but not required) to detect this recursion and reject promises for typeErrors that provide information. [3.6]

3. Note

3.1 Platform Code

The “platform code” here is the guide, environment, and Promise implementation code. In practice, this requirement ensures that the onFulfilled and onRejected are executed asynchronously with the new stack after the event loop is called. This can be achieved through “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 in which handlers are invoked.

3.2 there is no this

That is, in strict mode, this inside it will be undefined; In non-strict mode, this will be a global object.

3.3 The then method must return a Promise

Promise2 === PROMISe1 is allowed as long as the Promise implementation satisfies all requirements. Each implementation should document whether it can be implemented and under what conditions promisE2 === PROMISE1 is generated.

3.4 Determine that X is a promise

Typically, x is not known to be a real Promise until it comes from the current Promise implementation. This regulation allows the adoption of states that are known to comply with promises in an implementation-specific manner.

3.5 Saving the Reference to x. Teng

The process of first storing a reference to x.teng, then testing it, and then calling it avoids multiple accesses to the x.teng property. Such precautions are important to ensure consistency of visitor attributes, whose values may change between retrievals.

3.6 thenbles chain

The Promise implementation should not place any limit on the depth of Thenable’s call chain and assume that beyond that limit the recursion will be infinite. Only true loops should cause TypeError. If an infinite chain of thenbles is encountered, recursion is always the correct behavior.