This is the third day of my participation in Gwen Challenge
Promise is a landmark feature update in the evolution of asynchronous JavaScript programming that was first proposed and implemented by the community until the release of ES6, which standardized its use and provided Promise objects natively
- through
then
Function chain call mode, in the form of a preliminary solutionThe callback hellThe problem of - Become a subsequent
async/await
,fetch
And so on
Promise features
- Asynchronous tasks
- Value through
resolve/reject
No interruption of executioncatch
和then rejectHandle
The difference between
Asynchronous tasks
new Promise
It is executed synchronouslyPromise
的then
catch
finally
Is executed asynchronously
1 -> 3 -> 2 -> 4
console.log(1)
new Promise((resolve) = > {
console.log(3)
resolve(4)
})
.then((data) = > {
console.log(data)
})
console.log(2)
Copy the code
Value through
The Promise’s then catch parameter is expected to be a function, and value penetration occurs when passing a non-function
Promise.resolve(1)
.then(2)
.then(Promise.resolve(3))
.then(console.log) / / 1
Copy the code
Resolve /reject Does not interrupt execution
Resolve/Reject simply changes the state of the Promise object and does not interrupt the current execution. We sometimes subconsciously think that resolve/ Reject directly moves the program to the next phase, but it does not
// The following code output order: 1 -> 3 -> 2
new Promise((resolve) = > {
console.log(1)
resolve(2)
console.log(3)
})
.then((data) = > {
console.log(data)
})
Copy the code
Catch and then rejectHandle
catch
Can capturePromise resolve
Before thethrow Error
errorthen rejectHandler
Unable to capture siblingsthen resolveHandler
Errors in the
new Promise((resolve) = > {
resolve(1)
throw new Error('err') // Will not enter a Promise catch
})
new Promise((resolve) = > {
resolve()
})
.then(() = > {
throw new Error('err')},() = > {
Failed to catch error err in then rejectHandler of the same class
})
Copy the code
Promise static method feature
Promise.all
The promise.all argument takes an iterable (usually an array) and returns a new Promise object. MDN: promise.all ()
- If the argument is an empty array, enter
then
- If the parameter array does not contain
Promise
Object into then, for examplePromise. All ([1, 2, 3])
- If an element
Promise reject
, will enter immediatelyPromise.all
的catch
, but does not affect the continuation of the Promise of other elements - If you want to know which one failed, you can find it in the element
Promise
的reject
Identified in
If the array element Promise handles its own internal error and returns a resolve Promise, it will not result in a promise.all catch
Promise.race
The promise.race parameter is the same as promise.all. Refer specifically to MDN: promise.race ().
- If the argument is an empty array,
then
catch
Don’t perform - If the array is not empty, based on the first element returned
Promise
的resolve
reject
Immediately corresponding to entryPromise.race
的then
catch
- No matter the first element
Promise
What is the result of, without affecting other elementsPromise
Continue to perform
Promise.allSettled
Promise. AllSettled introduces standard Promise from ES11. AllSettled waits for all element promises to complete (resolve or reject) and then
When can I enter promises.allsettled catch? At some Promise throw Error!
And promise.allsettled catch catches every error in a Promise, so it is possible to execute multiple times (Promise.all will only catch the first error).
const promise1 = new Promise(function(resolve, reject) {
setTimeout(() = > {
throw Error(1)},100)});const promise2 = new Promise(function(resolve, reject) {
setTimeout(() = > {
throw Error(2)},200)});Promise.allSettled([promise1, promise2])
.then((value) = > {
console.log('then', value)
})
.catch((err) = > {
// a promise1 error is captured after 100ms and a promise2 error is captured after 200ms
console.log('catch', err)
})
Copy the code
Promise.any
Promise.any is introduced by ES12
- If the element
Promise
There is aresolve
, I entered aPromise.any
的then
- If the element
Promise
All of themreject
, I entered aPromise.any
的catch
Promise/A + specification
As mentioned above, Promises are standard native objects provided by ES6. When our code needs to be compiled into ES5 or below, we need to introduce Polyfill. The Promise in Polyfill is written according to the Promise/A+ specification
See the Promise/A+ specification for details
- a
Promise
Must be in one of the following three states: Wait state (Pending
), execution state (Fulfilled
) and rejection states (Rejected
)- In the wait state,
promise
You can migrate to the execute or reject state - In the execution state,
promise
You cannot migrate to any other state and must have an immutable end value - In the rejection state,
promise
You cannot migrate to any other state and must have an immutable data cause
- In the wait state,
- The THEN method must be implemented
then
The function takes two argumentsthen
Function argument if it is not a function, the argument is ignoredthen
The function can be separated by apromise
Multiple calls- Must return one
promise
The instance
Write in the last
All and promise.race, one Promise reject interrupts the other promises. I look up the documents and write demo tests, all of which prove that neither interrupts nor waits. Hope to give a demo in the comments section