Jay
Jay

Reputation: 9582

Why does babel turn async functions into generators for node 8.10?

If node 8.10 supports async/await, why does babel transform async functions into generators?

babel translates:

const foo = async () => {
  const b = await bar()
  console.log(b)
}

into:

const foo = (() => {
  var _ref2 = (0, _asyncToGenerator3.default)(function* () {
    const b = yield bar();
    console.log(b);
  });

  return function foo() {
    return _ref2.apply(this, arguments);
  };
})()

this is my babel config:

  "babel": {
    "plugins": [
      "source-map-support",
      "transform-runtime"
    ],
    "presets": [
      [
        "env",
        {
          "targets": {
            "node": "8.10"
          }
        }
      ],
      "stage-3"
    ]
  }

Upvotes: 0

Views: 435

Answers (1)

loganfsmyth
loganfsmyth

Reputation: 161517

You've opted into compilation of async functions by enabling "stage-3". The env preset is ignoring them, based on the config, but stage-3 is opting back in again. This, among other reasons, is why we've dropped the stage-X presets entirely in Babel 7.x, because they are hard to understand and you rarely know what they are actually doing.

My recommendation would be to remove the stage-3 preset, and if there are plugins in there that you actually want, add them on a case-by-case basis.

Upvotes: 1

Related Questions