关于前端:发送异步请求后执行顺序问题asyncawait

解决方案

 getInfo() {
        var _this = this
        return new Promise((resolve, reject) => {
          this.getRList(function(res) {
            _this.regionList = res
            resolve(_this.regionList)
          })
        })
      },
      getInfo1() {
        var _this = this
        this.getInfo().then(val => {
          _this.getCList(function(res1) {
            _this.cityList = res1
            _this.privListdata = _this.resetPrivList(val, res1)
          })
        })
      },

实践根底

1.async/await应用办法

用Promise来实现。
Promise是ES6的新个性,用于解决异步操作逻辑,用过给Promise增加then和catch函数,解决胜利和失败的状况
示例一:

function2(){
    return new Promise((resolve, reject) => {
      //你的逻辑代码
      resolve(/* 这里是须要返回的数据 */)
    });
 } 
function3(){
  return new Promise((resolve, reject) => {
    //你的逻辑代码
    resolve(/* 这里是须要返回的数据 */)
  });
} 

// 调用 
function1(){ 
  this.function2().then(val => {
    this.function3(); 
  }); 
}

val为resolve办法返回的数据

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理