在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/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('舊密碼驗(yàn)證通過')
        } else {
          console.log('舊密碼輸入錯(cuò)誤')
        }
      } else {
        console.log('密碼不能為空')
      }
    }

data中 status: false

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

但是實(shí)際輸出順序是:
111
333
false
舊密碼輸入錯(cuò)誤
222

這是為什么?

回答
編輯回答
毀與悔

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

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

因?yàn)槟愕?code>console.log(333)是在catch外面的。。
如果看不懂的話,學(xué)習(xí)一下Promise

2017年10月12日 22:09