Through the official website. We found the update for 2021 12.

First, the replaceAll

We used to replace specified characters in a string with a re

let str = "she is a good teather and a good mother";
// she is a bad teather and a bad mother
let temp = str.replace(/good/g,'bad');
Copy the code

With replaceAll

let str = "she is a good teather and a good mother";
// she is a bad teather and a bad mother
let temp = str.replaceAll('good','bad');
Copy the code

Second, the Promise. Any

Before we do that, let’s go over a few of Promise’s methods

Promise.all———— wraps multiple Promise instances into a new Promise instance. Also, success and failure return different values, with success returning an array of results and failure returning the first rejected state.

const promises = [ Promise.resolve('result A'), Promise.resolve('result B'), Promise.resolve('result C'), ] const promises1 = [ Promise.resolve('result A'), Promise.reject('ERROR B'), Promise.reject('ERROR C'), ] Promise. All (promises). Then (res => {//promises enter console.log(res, 'resolve all! ')}). Catch (err => {//promises1 enter console.log(err, 'There is a reject! ')});Copy the code

In particular, the order of the data in the array of the successful results obtained by Promise.all is the same as that received by Promise.all, that is, the result of P1 is first, even though the result of P1 is obtained later than p2. This has one great benefit: When developing request data on the front end, there are occasional scenarios where multiple requests are sent and the data is retrieved and consumed according to the order in which they are requested, using Promise.all is a surefire way to solve this problem. Success in all, success in none.

Promise.race———— Whoever succeeds or fails first is considered the success or failure of promise. race.

Const promises2 = [promise.reject ('ERROR B'), promise.resolve ('result A'), Promise.reject('ERROR C'),] promise.race (promises2). Then (res => {// If result A returns the fastest. Console. log(res, 'I finished first! ') }).catch(err => { // ERROR ... Results return fastest. Console. log(err, 'I reject! ')});Copy the code

Promise.allSettled(ES2020)

The biggest problem with Promise.all is that when a task dies, all tasks die. Promise goes into reject. Promise.allsettled solves this problem and will return to execute. The promise.allSettled method returns a Promise that is resolved after all given promises have been resolved or rejected, and each object describes the outcome of each Promise.

const P1 = new Promise((resolve, reject) => { axios.get('./data1.json').then((res) => { resolve(res) }).catch(err => { reject(err) }); }) const P2 = new Promise((resolve, reject) => { axios.get('./data2.json').then((res) => { resolve(res) }).catch(err => { reject(err) }); }) promise.allSettled ([P1, P1]).then(res => {console.log(res, 'No matter what the future holds, we will always be together! ')});Copy the code

So, there is no catch execution in Promise.AllSettled. The request is executed regardless of success. Then the user decides to filter, which makes it easier for users to accept.

Promise.any(ES2021)

If one of the promises succeeds, the promise that succeeded is returned. If none of the promises in the iterable succeed (i.e. all Promises fail/reject), return an instance of the failed promise and AggregateError type, which is a subclass of Error and is used to group single errors together

const promises = [ Promise.reject('ERROR A'), Promise.reject('ERROR B'), Promise.resolve('result'), // There is a resolve to enter console.log(res, 'I succeed first! ')}). Catch (err => {// reject) console.log(err, 'I reject! ')});Copy the code

Comparison of promise methods

Promise.any() contrast with promise.all ()

They are opposite to each other

Promise.all(): The first input rejection will reject the result Promise or its implementation value as an array of input implementation values. Promise.any(): The first input implementation satisfies the result Promise or its rejection value is an array of input rejection values.

There are different emphases

Promise.all() is interested in all success. The opposite situation (at least one rejection) leads to rejection. Promise.any() is interested in the first implementation. The opposite (rejection only) leads to rejection.

Promise.any() contrasts with promise.race ()

Promise.race() is interested in completion. The first Promise fulfilled was “Victory.” In other words: we want to know what asynchronous computation is done first.

Promise. Any () is interested in success. The first Promise of success is “victory”. In other words: we want to know the first successful asynchronous computation.

Promise.all() contrast with promise.allSettled

Promise.allsettled is similar to promise.all in that its parameters accept an array of promises and return a new Promise, except that allSettled will not be settled on a short circuit.

conclusion

If the article is not well written, welcome to comment correction, also welcome to discuss the problem!