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