async-await是ES7的语法,截止我写这篇文章为止,小程序还是不支持async-await语法的,所以需要使用regenerator这个库
在小程序开发工具中如果勾选es6转es5, 会报错:
1
ReferenceError: regeneratorRuntime is not defined
为了避免报错引入regenerator。
引入步骤
-
在根目录下创建 lib 文件夹,并将 https://github.com/facebook/regenerator/tree/master/packages 里面的 regenerator-runtime 文件夹放进去。
-
如果出现错误
Uncaught TypeError: Function(...) is not a function将runtime.js最后一段try..catch...删掉
使用方法
-
在需要使用的地方直接进入即可
1
import regeneratorRuntime from '../../utils/regenerator-runtime/runtime';
-
当有返回是
promise时,可直接使用asyncawait -
处理微信微信小程序原生Api可以使用
promise包裹一层,该api可以暴露在app.js中并通过getApp()获取,也可以通过exportimport来实现。
在需要使用api 的页面中处理如下
1 |
import regeneratorRuntime from '../../utils/regenerator-runtime/runtime'; |
参考
https://blog.csdn.net/weixin_33755554/article/details/88760981
https://www.cnblogs.com/cckui/p/10231801.html

