亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

關于promise函數的用法,我這樣寫是對的嗎?

關于promise函數的用法,我這樣寫是對的嗎?

元芳怎么了 2019-03-22 22:19:50
如下,我有兩個函數,寫成了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錯誤是第二個函數中的錯誤,而不是第一個函數的,這是為什么,如何才能拋出所有函數的錯誤呢?
查看完整描述

2 回答

?
神不在的星期二

TA貢獻1963條經驗 獲得超6個贊

Promise.all(iterable) 方法返回一個 Promise 實例,此實例在 iterable 參數內所有的 promise 都“完成(resolved)”或參數中不包含 promise 時回調完成(resolve);如果參數中 promise 有一個失敗(rejected),此實例回調失?。╮eject),失敗原因的是第一個失敗 promise 的結果。
MDN

Promise.all只會返回第一個被rejected的結果。


查看完整回答
反對 回復 2019-04-10
  • 2 回答
  • 0 關注
  • 443 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號