关于vue异步接口请求

方式一

getData: async function() { // 异步方法
  try { // 顺序请求
    await this.getSetupList();
    await this.getRoleList();
    await this.getList();
  } catch (e) {}
}

方式二

login: function(code) { // 定义方法
  return new Promise((resolve, reject) => {
    console.log(code)
    getThirdOpenid({ code: code }).then(response => { // 请求接口
      console.log(response)
      return response // 正确返回
    }).catch(res => {
      reject('获取失败') // 错误返回
    })
  })
}

login(code).then(res => { // 调用异步方法
  // 返回正确的操作
 }).catch(res => {
  // 返回错误的操作
})

方式三

function login(code) {
  return new Promise((resolve, reject) => { // 异步方法
    getData1().then(response => { // 方法一
      return response // 返回正确值
    }).then((res) => {
      getData2().then((response) => { // 方法一返回正确后执行方法二
        return response // 返回正确值
      }).then((res) => {
        getData3().then(response => { // 方法二返回正确后执行方法三
          return response // 返回正确值
        }).then(res => {
          getData4().then(response => { // 方法三返回正确后执行方法四
            resolve(response ) // 方法执行完毕,抛出最后结果或进行某些操作
          }).catch(res => {
            reject('方法四返回错误')
          })
        }).catch(res => {
          reject('方法三返回错误')
        })
      }).catch(res => {
        reject('方法二返回错误')
      })
    }).catch(res => {
      reject('方法一返回错误')
    })
  })
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。