小程序原生使用ES7 async / await 語法
小程序開發(fā)工具-詳情-開啟ES6轉(zhuǎn)ES5
下載 regenerator 庫 https://github.com/facebook/regenerator
將庫中packages文件夾下 regenerator-runtime 文件夾全部復(fù)制到小程序項(xiàng)目中
小程序項(xiàng)目全局引入 regenerator 庫
在app.js中引入
const regeneratorRuntime = require('./libs/runtime-module.js')使用方法
Page({ /** * 頁面的初始數(shù)據(jù) */ data: { num: 0 }, /** * 生命周期函數(shù)--監(jiān)聽頁面加載 */ async onLoad(options) { this.testing() this.promiseFn() await this.testAsync() }, testing() { console.log('test') }, promiseFn() { this.testPromise().then((res) => { console.log(res) }) }, testPromise() { return new Promise((resolve, reject) => { setTimeout(() => { console.log('Promise handle') resolve(123) }, 2000) }) }, async testAsync() { const result = await this.testPromise() console.log('async test--', result) }})以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持錯(cuò)新站長站。
|
新聞熱點(diǎn)
疑難解答
圖片精選