简体   繁体   English

使用async / await没有babel-polyfill

[英]using async/await without babel-polyfill

my code is following: 我的代码如下:

  @action async login(payload){
    try {
      this.loginLoading = true
      const data = await request('/admin/login', {
        method: 'post',
        data: payload
      })
      this.logined = true
      localStorage.setItem('token', data.token)
      this.loginLoading = false
    } catch (error) {
      console.log(error)
      message.error('login failed')
      this.logined = false
      this.loginLoading = false
    }

babel-polyfill will transform above code to: babel-polyfill会将以上代码转换为:

       return _regenerator2.default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.prev = 0;

                this.loginLoading = true;
                _context.next = 4;
                return (0, _request2.default)('/admin/login', {
                  method: 'post',
                  data: payload
                });

              case 4:
                data = _context.sent;

                this.logined = true;
                localStorage.setItem('token', data.token);
                this.loginLoading = false;
                _context.next = 16;
                break;

              case 10:
                _context.prev = 10;
                _context.t0 = _context['catch'](0);

                console.log(_context.t0);
                _message2.default.error('login failed');
                this.logined = false;
                this.loginLoading = false;

              case 16:
              case 'end':
                return _context.stop();
            }
          }

the transformed code will rename variables that hard to read and debug in chrome devl-tool using source map, actually latest chrome is support async/await syntax, so I do not wanna to use babel-polyfill in development. 转换后的代码将使用源映射重命名chrome devl-tool中难以读取和调试的变量,实际上最新的chrome支持async / await语法,所以我不想在开发中使用babel-polyfill。

But if remove babel-polyfill, will throw regeneratorRuntime is not defined. 但是如果删除babel-polyfill,会抛出reinratorRuntime regeneratorRuntime is not defined.

If you have set your browser targeting to chrome >= 59 then you simply will use the native generators and will never make use of the polyfill. 如果您已将浏览器定位设置为chrome> = 59,那么您只需使用本机生成器,并且永远不会使用polyfill。

https://medium.com/@zwacky/add-es7-async-await-support-into-your-non-bleeding-edge-build-process-ad0dded0d002 https://medium.com/@zwacky/add-es7-async-await-support-into-your-non-bleeding-edge-build-process-ad0dded0d002

Interesting to note: 有趣的是:

If you only need the generator polyfill — which is needed for async/await — then you can just use facebook/regenerator , which is used by babel-polyfill anyway. 如果你只需要生成器polyfill - 这是async / await所需的 - 那么你可以使用facebook / regenerator ,无论如何它都被babel-polyfill使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM