The primary way to interact with a Promise is by passing a function into its then method to get the final value of the Promise or why the Promise ultimately rejects it

The term

  • A promise is an object or function that contains the then methods compatible with the Promise specification,
  • Thenable is an object or function that contains the THEN method.
  • Value is any Javascript value. (including undefined, thenable, promise, etc.)
  • Exception is the value thrown by the throw expression.
  • Reason is a value that describes why a Promise was rejected.

requirements

2.1 Promise state

A Promise must be in one of these states: Pending, depressing, or Rejected

  • If the state is pending, then the promise can switch to the fulfilled or Rejected state
  • If this is a pity state, then the promise cannot be changed into any other state. There must be a value and it cannot be changed
  • If it is the Rejected state, then the Promise cannot be converted to any other state. There must be a reason, and the value cannot be changed

“Value cannot be changed” means that its identity cannot be changed, not that its member contents cannot be changed

2.2 then method

A Promise must provide a THEN method to retrieve its value or cause. Promise’s then method accepts two arguments:

jspromise.then(onFulfilled, onRejected)
Copy the code
  1. onFulfilledonRejectedBoth are optional parameters:
    1. ifonFulfilledIf it is not a function, ignore it.
    2. ifonRejectedIf it is not a function, ignore it.
  2. ifonFulfilledIs a function:
    1. It must be inpromise fulfilledCall after, andpromisethevalueIs its first argument
    2. It can’t be inpromise fulfilledBefore the call
    3. Cannot be called more than once
  3. ifonRejectedIt’s a function
    1. It must be inpromise rejectedCall after, andpromisethereasonIs its first argument
    2. It can’t be inpromise rejectedBefore the call
    3. Cannot be called more than once
  4. onFulfilledonRejectedOnly allowed inexecution contextThe stack runs only when it contains platform code
  5. onFulfilledonRejectedMust be treated as a function call (i.e. inside a function)thisundefined)
  6. For apromise, it’sthenMethod can be called multiple times
    1. whenpromise fulfilledAfter allonFulfilledMust be executed in the order of their registration
    2. whenpromise rejectedAfter allOnRejectedMust be executed in the order of their registration
  7. thenMust return onepromise [3.3]
    1. ifonFulfilledonRejectedReturns the valuex, the implementationPromiseAnalytical process[[Resolve]](promise2, x)
    2. ifonFulfilledonRejectedIf e is thrown, thenpromise2It should be ereasonBe rejected.
    3. ifonFulfilledIt’s not a function andpromise1alreadyfulfilled,promise2Have to bepromise1The value of thefulfilled.
    4. ifOnRejectIt’s not a function andpromise1alreadyrejected,promise2Must be the samereasonBe rejected.
jspromise2 = promise1.then(onFulfilled, onRejected);
Copy the code

2.3 Promise resolution process

The Promise resolution process is an abstract process with a Promise and a value as parameters, which can be expressed as [[Resolve]](Promise, x). The process is as follows;

  1. If the promise and x point to the same value, reject the promise using TypeError as a reason.

  2. If X is a Promise, take its state [3.4]

    1. If X is pending, the promise must keep pending until X depressing or Rejected
    2. If X is a pity state, the value of x will be used as fulfill Promise
    3. If x is the Rejected state, use the x reason for reject promise
  3. If x is an object or a function

    1. Assign then to x.teng. [3.5]
    2. If an exception is thrown when taking the value x.teng, the promise is rejected as the reason for that exception
    3. If then is a function, then is called with x as this, and the first argument is resolvePromise, the second argument is rejectPromise, and:
      1. When resolvePromise is called with argument y, execute [[Resolve]](promise, y).
      2. When a rejectPromise is invoked with an r argument, reject the promise for r reasons.
      3. If both resolvePromise and rejectPromise are invoked, or if they are invoked more than once, then only the first time is valid and the rest is ignored.
      4. If an exception is thrown when calling THEN
        1. If a resolvePromise or rejectPromise has already been invoked, it is ignored
        2. Otherwise, the promise is rejected for reason
  4. If x is neither an object nor a function, x is taken as the fulfill promise.

Refer to the link

  • Understanding Promise in depth (PART 1)
  • Promise/A+ standard Chinese
  • Promise/A+ Standard English
  • Test the warehouse