<强>小程序原生使用ES7 async/等待语法强>
小程序开发工具,详情——开启ES6转ES5
下载蓄热器库https://github.com/facebook/regenerator
将库中包文件夹下regenerator-runtime文件夹全部复制到小程序项目中
小程序项目全局引入再生器库
在应用。js中引入
const regeneratorRuntime=要求(“。/libs/runtime-module.js”) >之前使用方法
页面({/* * *页面的初始数据 */数据:{ num: 0 },/* * *生命周期函数——监听页面加载 */异步onLoad(选项){ this.testing () this.promiseFn () 等待this.testAsync () }, 测试(){ console.log(测试) }, promiseFn () { this.testPromise () ((res)=比;{ console.log (res) }) }, testPromise () { 返回新的承诺((解决,拒绝)=比;{ setTimeout(()=比;{ 控制台。日志(“承诺处理”) 解决(123) },2000) }) }, 异步testAsync () { const结果=等待this.testPromise () 控制台。日志(“异步测试——”,结果) } }) >之前以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
详解小程序原生使用ES7 async/等待语法