Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transform async functions with regenerator #332

Merged
merged 3 commits into from
Aug 2, 2016

Conversation

fson
Copy link
Contributor

@fson fson commented Aug 2, 2016

Remove transform-async-to-generator, which to my understanding is meant
to be used in environments that support generators natively.

Because we're compiling generators to ES5 anyway, we can simply use
regenerator to transform async functions too, which results in
slightly simpler output and only uses the regenerator runtime instead
regenerator runtime + _asyncToGenerator Babel helper.

Tested with the example code from #327.

Output (without minification) before:

{
  key: 'componentDidMount',
  value: function () {
    var _ref = _asyncToGenerator(_regenerator2.default.mark(function _callee() {
      return _regenerator2.default.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.prev = 0;
              _context.next = 3;
              return fetch('http://google.com');

            case 3:
              _context.next = 8;
              break;

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

              console.log('oh noes');

            case 8:
            case 'end':
              return _context.stop();
          }
        }
      }, _callee, this, [[0, 5]]);
    }));

    function componentDidMount() {
      return _ref.apply(this, arguments);
    }

    return componentDidMount;
  }()
}

After the change:

{
  key: 'componentDidMount',
  value: function componentDidMount() {
    return _regenerator2.default.async(function componentDidMount$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.prev = 0;
            _context.next = 3;
            return _regenerator2.default.awrap(fetch('http://google.com'));

          case 3:
            _context.next = 8;
            break;

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

            console.log('oh noes');

          case 8:
          case 'end':
            return _context.stop();
        }
      }
    }, null, this, [[0, 5]]);
  }
}

Remove `transform-async-to-generator`, which to my understanding is meant
to be used in environments that support generators natively.

Because we're compiling generators to ES5 anyway, we can simply use
`regenerator` to transform async functions too, which results in
slightly simpler output and only uses the regenerator runtime instead
regenerator runtime + _asyncToGenerator Babel helper.
@ghost ghost added the CLA Signed label Aug 2, 2016
require.resolve('babel-plugin-transform-class-properties'),
require.resolve('babel-plugin-transform-object-rest-spread'),
require.resolve('babel-plugin-transform-regenerator'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn’t -regenerator already a part of es2015?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is, but in preset-es2015 it's passed different options, { async: false, asyncGenerators: false }. The default options (used here) are:

{
  asyncGenerators: true,
  generators: true,
  async: true
}

(Note that asyncGenerators is true by default, but since we don't enable the async generator syntax plugin, this doesn't matter.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, so that’s how it works.

@@ -33,11 +33,12 @@
"babel-eslint": "6.1.2",
"babel-jest": "14.1.0",
"babel-loader": "6.2.4",
"babel-plugin-syntax-async-functions": "^6.8.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove carets from deps

@gaearon gaearon merged commit 6ea7ec5 into facebook:master Aug 2, 2016
@gaearon
Copy link
Contributor

gaearon commented Aug 2, 2016

Thanks.

@gaearon gaearon added this to the 0.2.3 milestone Aug 25, 2016
@gaearon gaearon mentioned this pull request Aug 25, 2016
@lock lock bot locked and limited conversation to collaborators Jan 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants