The introduction

As the saying goes, A good memory is better than A bad pen, so I decided to translate the Promise/A+ specification, to help myself more deeply understand the Promise, for the future Promise source code preparation, but also hope that this article will help you.

The body of the

This is an open source, interoperable JavaScript Promise specification that implementers provide for implementers

A promise represents the end result of an asynchronous operation. The primary way to interact with a promise is to register a callback function through the then method to receive the final value of a promise or the reason why the promise didn’t complete.

The specification details the behavior of the THEN method. It provides an interoperable foundation on which all Promises/A + promise-compliant implementations can rely. Therefore, the specification is considered to be very stable. Although Promises/A+ authors may occasionally modify the specification to make small backward compatible changes to address newly discovered problems, we will only integrate large or backward incompatible changes after careful consideration, discussion, and testing.

Promises/A+ historically, Promises/A+ clarified the act clause of early Promises/A proposals by expanding it to cover factual acts and omits unspecified or questionable parts.

Finally, the core of the Promises/A+ specification does not deal with how to create, complete, or fail Promises. Instead, it chooses to focus on an interoperable THEN approach. Future work in supporting specifications may address these topics.

1. The term

  • 1.1 Promise is an object or function with then methods, and its behavior conforms to the specification.

  • 1.2 Thenable is an object or function defined on the THEN method.

  • 1.3 Value is any valid JavaScript value (including undefined, thenable, or promise)

  • 1.4 Exception is a value thrown using a throw statement

  • 1.5 Reason is a value indicating why a promise failed

2. Demand

2.1 Promise state

  • 2.1.1 When the Promise state is Pending:

    • 2.1.1.1 The state can be completed or Failed
  • 2.1.2 When the promise state is fulfilled:

    • 2.1.2.1 Cannot change to any state

    • 2.1.2.2 There must be a value that cannot be changed

  • 2.1.3 When The Promise is Rejected:

    • 2.1.3.1 Cannot change into any state

    • 2.1.3.2 There must be a Reason and the reason cannot be changed

2.2 then method

A promise must provide a THEN method to access its current or final value or Reason.

A Promise’s then method takes two arguments:

promise.then(onFulfilled, onRejected)

Copy the code
  • 2.2.1 onFulfilled 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 It must be called after the promise completes, and then take the promise’s value as its first argument

    • 2.2.2.2 It cannot be invoked before the promise is completed.

    • 2.2.2.3 It cannot be called more than once.

  • 2.2.3 If onRejected is a function

    • 2.2.3.1 It must be called after a promise fails and then take the promise’s value as its first argument

    • 2.2.3.2 It cannot be invoked before the promise fails.

    • 2.2.3.3 It cannot be called more than once.

  • 2.2.4 Ondepressing and onRejected shall not be called before the execution context only contains the platform code [3.1]

  • 2.2.5 onFulfilled and onRejected must be called as a function (i.e. without this value) [3.2]

  • The 2.2.6 THEN method may be called multiple times in the same promise

    • 2.2.6.1 If/when the promise is completed, all corresponding ondepressing callbacks must be performed in the original then order

    • 2.2.6.2 If/when the Promise fails, all corresponding onRejected callbacks must be executed in the original THEN order

  • 2.2.7 THEN method must return a promise promise2 = promise1. Then (ondepressing, onRejected);

    • 2.2.7.1 If onFulfilled and onRejected return a value x, please run Promise Resolution Procedure [[Resolve]](promise2, x).

    • 2.2.7.2 If onFulfilled and onRejected throws an exception E, promise2 must reject eas reason

    • 2.2.7.3 If ondepressing is not a function and promise1 is completed, promise2 must be completed with the same value as promise1

    • 2.2.7.4 If onRejected is not a function and promise1 fails, promise2 must use the same reason as promisE1

2.3 Promise Solution

The promise resolution process is an abstract operation that takes the input promise and value, called [[Resolve]](promise, x). If x is thenable, assuming x behaves a bit like a promise, it tries to make the promise adopt the state of X. Otherwise, it will use the x value to fulfill the promise.

The handling of Thenable allows interoperability using Promises as long as there are Promises/A+ compatible THEN methods. It also allows Promises/A+ implementations to “assimilate” inconsistent implementations using sound THEN methods.

Run [[Resolve]](promise, x) to perform the following steps:

  • 2.3.1 If a Promise and X reference the same object, TypeError is used as 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 remain pending until X succeeds or fails

    • 2.3.2.2 If x is complete, complete the promise with the same value

    • 2.3.2.3 If x is in failed state, reject the promise with the same reason

  • 2.3.3 If x is an object or function

    • 2.3.3.1 Let THEN be X.teng [3.5]

    • 2.3.3.2 If the result of retrieving the attribute x. teng throws an exception e, use eas reason to reject the promise

    • 2.3.3.3 If then is a function, call x as this, with the first arguments resolvePromise and the second arguments rejectPromise, where:

      • 2.3.3.3.1 If resolvePromise is called by y, run [[Resolve]](promise, y)

      • 2.3.3.3.2 Reject the promise with r if rejectPromise is invoked with reason for R

      • 2.3.3.3.3 If both resolvePromise and rejectPromise are called, or if multiple calls are made to the same parameter, the first call takes precedence and any further calls are ignored

    • 2.3.3.4 If then is not a function, use x to complete the promise

  • 2.3.4 If x is not an object or function, use X to fulfill the promise

If a promise is resolved using a transformable component of the loop chain participating in the loop, then the recursive nature of [[Resolve]](promise, thenable) causes [[Resolve]](promise, thenable) to be called again, Following the above algorithm results in infinite recursion. Implementations are encouraged (but not required) to detect this recursion and reject promises with typeErrors containing information as reason. [3.6]

3. Notes

3.1

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, then is called after the event loop, and a new stack is used. This can be done through the setTimeout or setImmediate macro task mechanism, or using the MutationObserver or Process. nextTick microtask mechanism. Because the Promise implementation is considered platform code, it may itself contain a task scheduling queue or “trampoline” in which handlers are invoked.

3.2

That is, in strict mode, this is undefined, in loose mode, it is a global object.

3.3

Promise2 === PROMISE1 can be implemented as long as all requirements are met, and each implementation should document whether it can produce promisE2 === PromisE1 and under what conditions

3.4

In general, x is only known to be a true promise if it comes from the current instance, and this clause allows specific implementations to adopt states that are known to conform to promises.

3.5

The procedure first stores a reference to x. Chen, then tests it, and then calls the reference. Multiple accesses to the x.teng property are avoided. These precautions are important to ensure consistency in visitor attributes, which can change in value between retrievals.

3.6

The implementation should not place any limit on the depth of the Thenable chain and assume that the recursion exceeds the limit. Only true loops cause TypeError, and if you encounter an infinite number of Thenable chains, recursion is always the correct behavior.

conclusion

Familiar with the promise specification helps us to deeply understand the promise principle, for later look at the source code, handwritten source code paving the way ~ for a long time not touch English, after a long time to do translation for the first time, if there is a bad translation, also want to point out ~ we learn together, common progress ~

Finally, share my public number “web front-end diary”, if you want to see the article in a more timely manner, immediately go to pay attention to wow 😍