What is it

PromiseA+ is an open, robust and versatile JavaScript Promise standard that ES6 promises and battery apis are developed using

Second, the term

  • Promise: is a legal object or function that has a then method
  • Thenable: is a function or object that defines the then method
  • Reson: Explains why the promise state is reject
  • Value: any parameter, including undefined then PROMISE
  • Exception: Indicates that the throw throws an exception

The promiseA+ specification you Must Know

1, the state

A promise must have three states: Pending, depressing, and Rejected

  • 1. Pending: At this time, the state of the promise can be changed into a pity or rejected state
  • This is a big pity: The state cannot be switched to another state, and a fixed data value will be returned
  • 3, Rejected: You cannot switch to the rejected state and there is an unchangeable reason

2. Then method

The promise will eventually provide a THEN method to access the currently returned value or reson. The then method takes two parameters promise.then(onFullfilled,onReject), both of which are optional.

  • 1, onFullfilled feature: called once before completion of execution, returns the first parameter called data successfully
  • 2. OnReject feature: Called once before completion, the first parameter returned is the reason for the call failure

This method is called only when onFullfilled and onReject are functions. If either of them is not a function, the then method automatically ignores non-function arguments and calls only function arguments

Number of calls to the THEN method: The THEN method can be called multiple times within the same promise. This is a big pity. When the state of the promise is fulfilled, all the onFulfilled will be fulfilled in sequence, and vice versa. The return value of the THEN method: The THEN method returns a promise

3. Promise method call procedure

Promise resolution is an abstract operation that takes a Promise and a value, which we express as [[Resolve]](Promise, x). If x has a then method and looks like a Promise, The resolver attempts to make the Promise accept x’s state; Otherwise it implements the promise with the value of x. This thenable feature makes Promise implementation more universal: as long as it exposes A THEN method that follows the Promise/A+ protocol; This also allows implementations that follow the Promise/A+ specification to coexist well with less formal but usable implementations. To run [[Resolve]](promise, x), follow these steps:

X is the promise case

  • X is equal to promise: If a promise and x point to the same object, reject the promise as TypeError
  • X to PromiseIf x is a Promise, make the Promise accept the state of x: Note 4
  • X is in the waiting state: Promise needs to remain in a wait state until x is implemented or rejected
  • X is in the execution state: executes the promise with the same value
  • X is in the rejected stateReject the promise with the same grounds

X is an object or a function

  • X is an object or a function: assigns x. teng to then

Throw error e when taking the value x. Chen: reject a promise based on e

  • If then is a function, x is called as the function’s scope this. Pass two callback functions as arguments, the first called resolvePromise and the second called rejectPromise:
    • If resolvePromise is called with the value y, run [[Resolve]](promise, y)
    • If rejectPromise is invoked with argument r, reject the promise with argument r
    • If both resolvePromise and rejectPromise are invoked, or if the same parameter is invoked more than once, the first call is preferred and the remaining calls are ignored
  • If calling the then method raises exception e:
    • If a resolvePromise or rejectPromise has already been invoked, ignore it or reject the promise based on e
    • If then is not a function, execute the promise with an x argument

X is not an object or a function

If x is not an object or function, execute the promise with x as an argument

If a promise is resolved by an object in a loop’s Thenable chain, and the recursive nature of [[Resolve]](promise, thenable) causes it to be called again, the algorithm above will lead to infinite recursion. The algorithm does not require it, but encourages the agent to detect the presence of such recursion, and if so, reject the promise with an identifiable TypeError as a justification.

4. Refer to the study documents

Promise:Promisesaplus.com/#terminolog…