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

9次阅读

共计 683 个字符,预计需要花费 2 分钟才能阅读完成。

解决方案

 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 办法返回的数据

正文完
 0