关于promise函数的用法,我这样写是对的吗?

如下,我有两个函数,写成了promis形式
函数一
verifyGA(type){
letthat=this;
returnnewPromise((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;
}
})
})
},
函数二
checkCode(type){
letthat=this;
letbind=this.isEmail?32:31;
letUntie=this.isEmail?34:33;
letcode_type=type==1?bind:Untie;
returnnewPromise((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;
}
})
})
},
现在我的需求是点击提交按钮的时候,去调用上面两个方法分别校验两个验证码是否正确,只有正确的情况下,才能去提交,于是我使用Promise.all()去处理这两个函数,不知道这样写对不对,如果错了,应该怎么写才对
提交函数
confirm(){
letthat=this;
Promise.all([this.verifyGA(12),this.checkCode(1)]).then(res=>{
console.log(res);
/*正常处理提交流程*/
}).catch(error=>{
console.log(error);
/*抛出错误*/
})
}
然后我发现如果上面两个函数都请求失败的时候,promise.all().catch()中抛出的error错误是第二个函数中的错误,而不是第一个函数的,这是为什么,如何才能抛出所有函数的错误呢?
红糖糍粑
浏览 712回答 2
2回答

12345678_0001

Promise.all(iterable)方法返回一个Promise实例,此实例在iterable参数内所有的promise都“完成(resolved)”或参数中不包含promise时回调完成(resolve);如果参数中promise有一个失败(rejected),此实例回调失败(reject),失败原因的是第一个失败promise的结果。MDNPromise.all只会返回第一个被rejected的结果。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript