vue项目中async、await+promise来将异步转为同步

时间:2020-06-18 18:46:02   收藏:0   阅读:485

在钩子函数中这样写:

mounted: function() {
  (async function(){
    console.log(1)
    var a = await that.testFunc1()
    console.log(a)
    console.log(3)
  })();
},
methods: {
  testFunc1: function() {
    return new Promise((resolve, reject)=>{
      this.testFunc2("","",res=>{
        resolve(res);
      });
    });
  },
  testFunc2: function(url,params,callback) {
    setTimeout(()=>{
      callback(2);
    },1000);
  }
}

 打印结果:

技术分享图片

 

原文:https://www.cnblogs.com/chenyoumei/p/13159036.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!