小程序如何支持使用 async/await

时间:2022-07-23
本文章向大家介绍小程序如何支持使用 async/await,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

下载 regenerator-runtime

npm i regenerator-runtime

如何使用

在小程序中,不认识 node_modules 文件夹,无法通过以下方法来直接找到包文件

import regeneratorRuntime form 'regenerator-runtime'

所以需要将 regenerator-runtime 中的 runtime.js(支持async/await的核心文件) 拿出来存放到一个文件夹中,通过一般的文件引入方式使用。

例:

import regeneratorRuntime from '../../lib/runtime.js'

值得一提的是导出的名字必须为 regeneratorRuntime ,否则无法使用async/await

最后

使用过程中如果出现一些意外,我在构建npm版中有记录到一个碰到的错误

使用async/await(构建npm版): https://www.cnblogs.com/chanwahfung/p/11503533.html