Am I right to write this about the use of the promise function?

as follows, I have two functions written in promis form

function one

verifyGA(type){
      let that = this;
      return new Promise((resolve,reject) => {
        that.$post("/user/verifyGA",{
          gaCode:that.gaCode,
          captchaType:type
        }).then(res=>{
          if (!res.code) {
            resolve(true)
          } else {
            reject(res.message)
            that.gaError = res.message;
          }
        })
      })
    },

function two

checkCode(type){
      let that = this;
      let bind = this.isEmail ? 32:31;
      let Untie = this.isEmail ? 34:33;
      let code_type = type == 1 ? bind:Untie;
      return new Promise((resolve,reject) => {
        that.$post("/user/checkCode",{
          code:that.code,
          codeType:code_type
        }).then(res=>{
          if (!res.code) {
            resolve(true)
          } else {
            reject(res.message)
            that.codeError = res.message;
          }
        })
      })
    },

now my requirement is that when I click the submit button, I call the above two methods to verify whether the two CAPTCHAs are correct, and only if they are correct, I can submit them. So I use Promise.all () to deal with these two functions. I don"t know if this is correct. If it is wrong, how to write it correctly

submit function

confirm(){
      let that = this;
      Promise.all([this.verifyGA(12),this.checkCode(1)]).then(res=>{
        console.log(res);
        /*  */
      }).catch(error=>{
        console.log(error);
        /*  */
      })
    }

then I found that if the above two function requests fail, the error error thrown in promise.all (). Catch () is the error in the second function, not the first function. This is why, how can I throw errors for all functions?


The

error error is the error of the second function. It should be because the reject of the second function executes earlier.

const a = () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      reject("a error");
    }, 300);
  });
}
const b = () => {
  return Promise.reject('b error')
}

Promise.all([a(), b()])
.then(data => {
  console.log(data);
})
.catch(err => {
  console.error(err);
});
// 
b error

Promise.all resolve triggers require all promise in the array to be resolve, and reject only needs any of these promise reject to trigger.
how do I catch all errors?

const a = () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      reject("a error");
    }, 300);
  });
}
const b = () => {
  return Promise.reject('b error')
}

Promise.all([a().catch(err => err), b().catch(err => err)])
.then(data => {
  console.log(data);
})
.catch(err => {
  console.error(err);
});
// 
[ 'a error', 'b error' ]

notice that the output here is actually console.log (data) execution, not console.error (err) execution

.
The
Promise.all (iterable) method returns a Promise instance. In this instance, if all promise in the iterable parameter "complete (resolved)" or if the parameter does not contain promise, the callback completes the (resolve);. If the parameter promise has a failed (rejected), this instance callback failed (reject), failed because of the result of the first failed promise.
MDN

Promise.all only returns the first result that is rejected.

Menu