鍍金池/ 問答/HTML/ 所有異步請求完成后的回調(diào)

所有異步請求完成后的回調(diào)

vue的項(xiàng)目中現(xiàn)在有這兩個(gè)方法,有個(gè)C方法,需要在這兩個(gè)異步請求的方法完成后才執(zhí)行,要怎么寫,新手求問,
這兩個(gè)方法都使用了es6的promise 和 axios這個(gè)庫

created() {
  this._getChannelList()
  this._getRecentContact()
},
method: {
    _getChannelList() {
      getChannelList(this.getCookie('sender'), this.getCookie('version')).then(res => {
        // 成功
      }).catch(err => {
        console.log(err)
      })
    },
    _getRecentContact() {
      getRecentContact(this.getCookie('sender')).then(res => {
        // 成功
      }).catch(err => {
        console.log(err)
      })
    },
    c() {
       // 需要在上面兩個(gè)方法都請求完畢才執(zhí)行
    }
}
回答
編輯回答
墻頭草

axios可以執(zhí)行多個(gè)并發(fā)請求

function getUserAccount() {
  return axios.get('/user/12345');
}

function getUserPermissions() {
  return axios.get('/user/12345/permissions');
}

axios.all([getUserAccount(), getUserPermissions()])
  .then(axios.spread(function (acct, perms) {
    // 兩個(gè)請求現(xiàn)在都執(zhí)行完成
  }));
2018年8月3日 11:18
編輯回答
骨殘心

可以用 Promise.all 來控制流程

2017年3月24日 02:25
編輯回答
旖襯

用 Promise 可以輕松搞定:

Promise.all([this._getChannelList(), this._getRecentContact()])
  .then(() => {
    this.c();
  });

既然被邀請了,那我順手推薦一下我的講堂吧:Promise 的 N 種用法,講解 Promise 的各種常見用法。

2017年3月19日 14:56