This is the 8th day of my participation in the First Challenge 2022. For details: First Challenge 2022.

Hello everyone, I am L. In the previous article, we summarized the object methods of a Promise, including then, catch, and finally. These are all methods from Promise’s prototype prototype.

๐Ÿ‘‰Promise

Today we summarize the Promise class methods, including the resolve, Reject, All, allSettled, Race, and Any methods.

Resolve method

The usage of promise.solve is equivalent to new Promise, and the resolve operation is performed to set the Promise object state to the depressing state.

const promise = Promise.resolve({name: 'haha'})
/ / equivalent to
/* const promise = new Promise((resolve, reject) => { resolve({name: 'haha'}) }) */
Copy the code

Resolve parameters

Arguments are common values or objects

const promise = Promise.resolve({name: 'haha'})
Copy the code

Parameter is a Promise

const promise = Promise.resolve(new Promise((resolve, reject) = > {
  resolve(111)}))Copy the code

The parameter is thenable

const promise = Promise.resolve({
  then: function(resolve, reject) {
      resolve(11)}})Copy the code

Reject method

Reject is equivalent to new Promise, reject, and sets the Promise object state to Rejected.

const promise = Promise.reject('rejected') 
/ / equivalent to
const promise = new Promise((resolve, reject) = > {
  reject('reject')})Copy the code

Any parameter passed in, no matter what type, is passed to the catch as an argument to the Rejected state.

All methods

The promise.all method combines multiple Pormise to form a new Promise. The new Promise state is determined by these multiple promises.

When all the Promise states become fulfilled, the new Promise state becomes fulfilled, and all the Promise return values are formed into an array.

const p1 = new Promise((resolve, reject) = > {
  setTimeout(() = > {
    resolve(111)},1000)})const p2 = new Promise((resolve, reject) = > {
  setTimeout(() = > {
    resolve(222)},2000)})const p3 = new Promise((resolve, reject) = > {
  setTimeout(() = > {
    resolve(333)},3000)})Promise.all([p1, p2, p3, 'aaa']).then(res= > {
  console.log('res', res);
}).catch(err= > {
  console.log('err', err);
})
Copy the code

When one of the Promise states changes to Rejected, the new Promise state changes to Rejected and takes the return value of the first Reject as an argument.

const p2 = new Promise((resolve, reject) = > {
  setTimeout(() = > {
    // resolve(222)
    reject(222)},2000)})Copy the code

The Promise object calls the catch method, calling back the reject value as a catch parameter.

AllSettled method

The all method has a flaw. When one of the promises changes to the Rejected state, the new Promise immediately changes to the corresponding Rejected state. Thus, we cannot obtain the corresponding results for the fulfilled and pending promises.

ES11 added promsie.AllSettled. Settled means stable.

This is a big pity, which is settled in September. All Texas will be settled in September, which is a big pity, or rejected. This Promsie must be a pity.

const p1 = new Promise((resolve, reject) = > {
  setTimeout(() = > {
    resolve(111)},1000)})const p2 = new Promise((resolve, reject) = > {
  setTimeout(() = > {
    reject(222)},2000)})const p3 = new Promise((resolve, reject) = > {
  setTimeout(() = > {
    resolve(333)},3000)})Promise.allSettled([p1, p2, p3]).then(res= > {
  console.log('res', res);
}).catch(err= > {
  console.log('err', err);
})
Copy the code

If we look at the print, the result is an array containing the results of each Promise, and the result is an object with the state and value of that Promise object.

Race method

The promise.race method pits multiple promises against each other in a race that uses the Promise’s results first.

const p1 = new Promise((resolve, reject) = > {
  setTimeout(() = > {
    resolve(111)},3000)})const p2 = new Promise((resolve, reject) = > {
  setTimeout(() = > {
    reject(222)},500)})const p3 = new Promise((resolve, reject) = > {
  setTimeout(() = > {
    resolve(333)},1000)})Promise.race([p1, p2, p3]).then(res= > {
  console.log('res', res);
}).catch(err= > {
  console.log('err', err);
})
Copy the code

Any way

The promise. any method is new in ES12 and is similar to the Race method.

The difference between them is that the any method will wait until a depressing state before deciding the state of the new Promise.

If all Promise states are Rejected, it will wait until all Promise states become Rejected.

If all Promise states are Rejected, an AggregateError is reported.

const p1 = new Promise((resolve, reject) = > {
  setTimeout(() = > {
    // resolve(111)
    reject(111)},1000)})const p2 = new Promise((resolve, reject) = > {
  setTimeout(() = > {
    // resolve(222)
    reject(222)},500)})const p3 = new Promise((resolve, reject) = > {
  setTimeout(() = > {
    // resolve(333)
    reject(333)},3000)})Promise.any([p1, p2, p3]).then(res= > {
  console.log('res', res);
}).catch(err= > {
  console.log('err', err);
})
Copy the code

The articles๐Ÿ‘‡ ๐Ÿ‘‡ ๐Ÿ‘‡

I Promise you a lot of things.

(1) I Promise you that I will make a Promise to you.

Summary of ES10 and ES11 common knowledge points

Summary of ES7 and ES8 common knowledge points

Summary of ES6 Common Knowledge Points (3)

Summary of ES6 Common Knowledge Points (2)

Summary of ES6 Common Knowledge Points (1)

The 2021 year-end summary of an entry-level front-end programming debugger