如下,我有兩個函數(shù),寫成了promis形式函數(shù)一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;}})})},函數(shù)二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;}})})},現(xiàn)在我的需求是點擊提交按鈕的時候,去調(diào)用上面兩個方法分別校驗兩個驗證碼是否正確,只有正確的情況下,才能去提交,于是我使用Promise.all()去處理這兩個函數(shù),不知道這樣寫對不對,如果錯了,應(yīng)該怎么寫才對提交函數(shù)confirm(){letthat=this;Promise.all([this.verifyGA(12),this.checkCode(1)]).then(res=>{console.log(res);/*正常處理提交流程*/}).catch(error=>{console.log(error);/*拋出錯誤*/})}然后我發(fā)現(xiàn)如果上面兩個函數(shù)都請求失敗的時候,promise.all().catch()中拋出的error錯誤是第二個函數(shù)中的錯誤,而不是第一個函數(shù)的,這是為什么,如何才能拋出所有函數(shù)的錯誤呢?
關(guān)于promise函數(shù)的用法,我這樣寫是對的嗎?
紅糖糍粑
2019-05-13 09:12:12