catch 

  • Catch is a callback function used to specify when an error occurs.
  • Catch () causes the callback to continue execution instead of killing js on an error.
  • Errors in Promise objects are penetrable, passing backwards until they are caught. That is, an error is always caught by the next catch statement.

Usage:

let promise = new Promise((resolve, reject) => {
    reject('🙅')
})
promise.then((data) => {
    console.log(data)
}).catch(e => {
    console.log('my' + e)
})Copy the code

Implementation idea:

We know that the then method, if the first method doesn’t write the corresponding method, will pass through to the next THEN method; In the second then, you can do it with a reject method. So catch is equal to the second then, and we don’t write resolve, we write reject.

So, we can add this method to the prototype:

catch (fn) {
    return this.then(null, fn)
  } Copy the code

all 

The promise.all method is used to wrap multiple Promise instances into a new Promise instance.

Usage:

Promise.all([p1, p2, p3]).then(function(values) {
  console.log(values);
}); Copy the code
  • All () takes an array as an argument. P1, P2 and P3 are all instance objects of Promise. In order for a Promise to become Resolved, it needs to be Resolved in P1, P2 and P3. If one of the states in P1, P2 and P3 is Rejected, P becomes Rejected.
  • Success and failure return different values, with success returning an array of results and failure returning the first rejected state

Implementation idea:

The all method stores an array, and then puts the results of p1, p2, p3 into the corresponding array. If there is an error, the callback fails directly.

Promise.all = (promises) => {
  return new Promise((resolve, reject) => {
    letresult = []; // The result of the final return valuelet index = 0;
    let processData = (key, y) => {
      index++
      result[key] = y;
      if(promises.length === index) { resolve(result); }}for (leti = 0; i < promises.length; i++) { promises[i].then(y => { processData(i, y); }, reject); }})}Copy the code

race

Promse.race is a race, which means that a Promise. Race ([P1, P2, P3]) returns the fastest result, regardless of whether the result itself is a success or a failure

Usage:

Promise.race([p1, p2, p3]).then(function(value) {
  console.log(value);
}); Copy the code

Implementation idea:

It is similar to the all method, but it does not wait for other instances to execute. P1, P2, and p3 execute the then method first.

Promise.race =(promises) => {
  return new Promise((resolve, reject) => {
    for (leti = 0; i < promises.length; i++) { promises[i].then(resolve, reject); }})}Copy the code

resolve 

Sometimes you need to turn an existing object into a Promise object, and the promise.resolve method does this.

Usage:

Promise.resolve('😄') Copy the code

The implementation is as follows:

Promise.resolve = function (data) {
  returnnew Promise((resolve,reject)=>{ resolve(data); })}Copy the code

reject

The promise.Reject (Reason) method also returns a new Promise instance with a state of Rejected.

Usage:

Promise.reject('❌) Copy the code

The implementation is as follows:

Promise.reject = function (data) {
  returnnew Promise((resolve, reject) => { reject(data); })}Copy the code

So far, we have implemented a fairly complete Promise (⌒_⌒).