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
onFulfilled
和onRejected
Both are optional parameters:- if
onFulfilled
If it is not a function, ignore it. - if
onRejected
If it is not a function, ignore it.
- if
- if
onFulfilled
Is a function:- It must be in
promise
fulfilled
Call after, andpromise
thevalue
Is its first argument - It can’t be in
promise
fulfilled
Before the call - Cannot be called more than once
- It must be in
- if
onRejected
It’s a function- It must be in
promise
rejected
Call after, andpromise
thereason
Is its first argument - It can’t be in
promise rejected
Before the call - Cannot be called more than once
- It must be in
onFulfilled
和onRejected
Only allowed inexecution context
The stack runs only when it contains platform codeonFulfilled
和onRejected
Must be treated as a function call (i.e. inside a function)this
为undefined
)- For a
promise
, it’sthen
Method can be called multiple times- when
promise fulfilled
After allonFulfilled
Must be executed in the order of their registration - when
promise rejected
After allOnRejected
Must be executed in the order of their registration
- when
then
Must return onepromise
[3.3]- if
onFulfilled
或onRejected
Returns the valuex
, the implementationPromise
Analytical process[[Resolve]](promise2, x)
- if
onFulfilled
或onRejected
If e is thrown, thenpromise2
It should be ereason
Be rejected. - if
onFulfilled
It’s not a function andpromise1
alreadyfulfilled
,promise2
Have to bepromise1
The value of thefulfilled.
- if
OnReject
It’s not a function andpromise1
alreadyrejected
,promise2
Must be the samereason
Be rejected.
- if
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;
-
If the promise and x point to the same value, reject the promise using TypeError as a reason.
-
If X is a Promise, take its state [3.4]
- If X is pending, the promise must keep pending until X depressing or Rejected
- If X is a pity state, the value of x will be used as fulfill Promise
- If x is the Rejected state, use the x reason for reject promise
-
If x is an object or a function
- Assign then to x.teng. [3.5]
- If an exception is thrown when taking the value x.teng, the promise is rejected as the reason for that exception
- If then is a function, then is called with x as this, and the first argument is resolvePromise, the second argument is rejectPromise, and:
- When resolvePromise is called with argument y, execute [[Resolve]](promise, y).
- When a rejectPromise is invoked with an r argument, reject the promise for r reasons.
- 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.
- If an exception is thrown when calling THEN
- If a resolvePromise or rejectPromise has already been invoked, it is ignored
- Otherwise, the promise is rejected for reason
-
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