Try to translate Promise A+ into Chinese! English version: Promise A+ My github link

Promise A+

This is an open source, robust, interoperable JavaScript Promise specification from implementers for implementers.

Promises represent the end result of asynchronous operations. The primary way to interact with a promise is through the then method, which registers callbacks 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, which can rely on all Promises/A + compliant promise implementations to provide an interoperable base library. Therefore, the specification can be considered very stable. Although Promises/A + organizations may occasionally revise this specification and make minor backward compatible changes to address newly discovered extremes. But we 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. Instead, it focuses on providing interoperable THEN methods. Future work in supporting specifications may address these topics.

1. The term

  • 1.1 “Promise” is availablethenMethod that behaves according to this specification.
  • 1.2 “Thenable” is the definitionthenThe object or function of a method.
  • 1.3 “value” is any valid Javascript value, (includingundefined, thenable promise)
  • 1.4 “exception” means usethrowThe value thrown by the statement
  • 1.5 “reason” is the value of the rejected promise

2. Request

2.1 Promise state

A promise must be in one of three states: pending, fulfilled, and rejected. This is a big pity.

2.1.1 When a Promise is in the request state (Pending)

  • 2.1.1.1 Promise can become a pity or rejected state

2.1.2 When the promise is fulfilled

  • 2.1.2.1 A PROMISE cannot be changed into any other state
  • 2.1.2.2 There must be a value that cannot be changed

2.1.3 When a Promise is in the Rejected state

  • 2.1.3.1 A PROMISE cannot change into any other state
  • 2.1.3.2 There must be a reason and the reason cannot be changed

2.2 thenmethods

A promise must provide then methods to access its current or final value or cause. Promise’s then method takes two arguments:

promise.then(onFulfilled, onRejected)
Copy the code

2.2.1 onFulfilledandonRejectedBoth are optional arguments:

  • 2.2.1.1 ifonFulfilledIt’s not a function. You have to ignore it
  • 2.2.1.1 ifonRejectedIt’s not a function. You have to ignore it

2.2.2 ifonFulfilledIs the function:

  • 2.2.2.1 This function must be inpromiseThis is a big pity. After completing this song, it will be called, and the song will be fulfilledpromiseAs its first argument
  • 2.2.2.2 This function is inpromiseThis can never be called until this is fulfilled
  • 2.2.2.2 This function must not be called more than once

2.2.3 ifonRejectedIs the function:

  • 2.2.2.1 This function must be inpromiseRejected is called and rejectedpromiseReason as its first argument
  • 2.2.2.2 This function is inpromiseMust not be called before Rejected
  • 2.2.2.2 This function must not be called more than once

2.2.4 Execution Context stack (execution context) must not be called until it contains only platform codeonFulfilledandonRejected 3.1

2.2.5 onFulfilledandonRejectedMust be called as a function (i.e. with no this value–>……) .3.2

2.2.6 thenCan be called multiple times within the same promise

  • 2.2.6.1 If/IfpromiseThis is a big pity, which is to be fulfilled graduallyonFulfilledCallbacks must be based on the originalthenOrder to call
  • 2.2.6.2 If/IfpromiseA) Rejected B) rejectedonRejectedCallbacks must be based on the originalthenOrder to call

2.2.7 thenYou must return a promise3.3

  promise2 = promise1.then(onFulfilled, onRejected);
Copy the code
  • 2.2.7.1 ifonFulfilledoronRejectedReturn a valuex, run the Promise Resolution Procedure [[Resolve]](promise2, x) 2.3
  • 2.2.7.2 ifonFulfilledoronRejectedThrow an exceptione.promise2You must be rejected and rejectedeAs a reason
  • 2.2.7.3 ifonFulfilledIt’s not a method, andpromise1This is a big pity.promise2Must be used withpromise1Fulfiled with the same value
  • 2.2.7.4 ifonRejectedIt’s not a method, andpromise1I have been rejected.promise2Must be used withpromise1I have rejected the offer for the same reason

2.3 Promise Solution

The Promise resolution process is an abstract operation that takes a promise and value as input, which we express as [[Resolve]] (Promise, x). If x is thenable, assuming x behaves at least somewhat like promise, it will try to get promise to adopt x’s state. Otherwise x will be used to fulfill the promise

This treatment of Thenables Promises/A + allows promise implementations to interoperate, as long as they expose A Promises/A + compatible method. It also allows Promises/A + implementations to “assimilate” inconsistent implementations using A reasonable THEN method.

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

2.3.1 ifpromiseandxReference the same objectTypeErrorReject as a reasonpromise.

2.3.2 ifxIs a promise, adopt the promise state3.4

  • 2.3.2.1 ifxPending is request status,promiseYou must keep pending untilxFulfilled or rejected
  • 2.3.2.2 ifxThis is a pity, which is fulfilled with the same value fulfillpromise
  • 2.3.2.2 ifxIt is rejected (rejected)promise

2.3.3 In addition, ifxIt’s an object or a method

  • 2.3.3.1 letxAs ax.then. 3.5
  • 2.3.3.2 If fetchx.thenProperty is an exceptionewitheReject as a causepromise
  • 2.3.3.3 ifthenIt’s a way to putxAs athisTo call it, the first argument isresolvePromise, the second parameter isrejectPromise, including:
    • 2.3.3.3.1 If/IfresolvePromiseIs a valueyCall, run[[Resolve]](promise, y)
    • 2.3.3.3.2 If/IfrejectPromiseBy a reasonrCall,rI reject you.promise
    • 2.3.3.3.3 ifresolvePromiseandrejectPromiseAre called, or multiple calls to the same parameter, with the first call executed and any further calls ignored
    • 2.3.3.3.4 If calledthenThrow an exceptione.
      • 2.3.3.3.4.1 ifresolvePromiseorrejectPromiseAlready called, ignored.
      • 2.3.3.3.4.2 Or, useeAs a reason, I reject it.promise
  • 2.3.3.4 ifthenIt’s not a functionx(fulfill)promise

2.3.4 ifxIs neither an object nor a functionx(fulfill)promise

If a promise is resolved by a Thenable, and that Thenable participates in the Thenable loop of the loop, The recursive feature of [[Resolve]](promise, thenable) eventually causes [[Resolve]](promise, thenable) to be called again. Following the above algorithm results in infinite recursion, and implementations are encouraged (but not required) to detect this recursion and reject it as reason with TypeError containing information 3.6

3. Note

3.1 “Platform Code”

onFulfilled
onRejected
then
setTimeout
setImmediate
MutationObserver
process.nextTick

3.2 In the absence of this

this

3.3 Then must return a Promise

promise2 === promise1
promise2 === promise1

About 3.4 x

x
x
promise

3.5 about x.t hen

x.then
x.then

3.6 What Can I Do about Thenable Chain

TypeError

My first translation, welcome criticism and correction. Author: Yanni Jia Nickname: Unusual rabbit