title: 异步编程

Taro 2.x 版本中使用 async-await 不再需要 @tarojs/async-await

Taro 支持使用 async functions 来让开发者获得不错的异步编程体验,开启 async functions 支持需要安装包 babel-plugin-transform-runtimebabel-runtime

  1. $ yarn add babel-plugin-transform-runtime --dev
  2. $ yarn add babel-runtime

随后修改项目 babel 配置,增加插件 babel-plugin-transform-runtime

  1. babel: {
  2. sourceMap: true,
  3. presets: [
  4. [
  5. 'env',
  6. {
  7. modules: false
  8. }
  9. ]
  10. ],
  11. plugins: [
  12. 'transform-decorators-legacy',
  13. 'transform-class-properties',
  14. 'transform-object-rest-spread',
  15. ['transform-runtime', {
  16. "helpers": false,
  17. "polyfill": false,
  18. "regenerator": true,
  19. "moduleName": 'babel-runtime'
  20. }]
  21. ]
  22. }

值得注意的事,使用 async functions 一定要记得按照开发前注意中提示的内容进行操作,否则会出现报错