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