鍍金池/ 問答/HTML/ axios 異步操作執(zhí)行順序

axios 異步操作執(zhí)行順序

submitPwd () {
      if (this.oldPwd !== '' && this.newPwd !== '' && this.password !== '') {
        console.log(111)
        axios.post(httpUrl.checkOldPwd, this.oldPwd)
        .then(res => {
          console.log(222)
          this.status = true
        })
        .catch(err => console.log(err))
        console.log(333)
        console.log(this.status)
        if (this.status) {
          console.log('舊密碼驗證通過')
        } else {
          console.log('舊密碼輸入錯誤')
        }
      } else {
        console.log('密碼不能為空')
      }
    }

data中 status: false

以上代碼為一個提交密碼的方法案例,理想狀態(tài)的輸出順序應該是:
111
222
333
true
舊密碼驗證通過

但是實際輸出順序是:
111
333
false
舊密碼輸入錯誤
222

這是為什么?

回答
編輯回答
毀與悔

axios是異步請求,在它外面且在下面的代碼不會等待它完成,會直接開始運行,而異步請求體里面的內容會在其請求成功或者失敗才執(zhí)行相應的代碼。

2017年10月27日 01:08
編輯回答
扯不斷

因為你的console.log(333)是在catch外面的。。
如果看不懂的話,學習一下Promise

2017年10月12日 22:09