简体   繁体   中英

Babel transpiling problem with env preset

so I recently started learning how to use the babel transpiler but I discovered one major difference with the presets. When I use the es2015 preset. The code is transpiled successfully and it works great in my browser but when I use the env-preset the code is also transpiled but doesn't work at all.

Below is the original code I wrote in ES6

const jokeEl = document.getElementById('joke');
const getJoke = document.getElementById('getJoke');

getJoke.addEventListener('click', generateJoke);

generateJoke();

async function generateJoke () {

    const jokeRes = await fetch('https://icanhazdadjoke.com/', {
        headers: {
            'Accept' : 'application/json'
        }
    });

    const joke = await jokeRes.json();

    jokeEl.textContent = joke.joke;
}


Here is the code that works when I use the es2015 preset.

'use strict';

var jokeEl = document.getElementById('joke');
var getJoke = document.getElementById('getJoke');

getJoke.addEventListener('click', generateJoke);

generateJoke();

async function generateJoke () {

    var jokeRes = await fetch('https://icanhazdadjoke.com/', {
        headers: {
            'Accept' : 'application/json'
        }
    });

    var joke = await jokeRes.json();

    jokeEl.textContent = joke.joke;
}


Then, here is the code that doesn't work and is generated from using the env preset.

"use strict";

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

var jokeEl = document.getElementById('joke');
var getJoke = document.getElementById('getJoke');
getJoke.addEventListener('click', generateJoke);
generateJoke();

function generateJoke() {
  return _generateJoke.apply(this, arguments);
}

function _generateJoke() {
  _generateJoke = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
    var jokeRes, joke;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return fetch('https://icanhazdadjoke.com/', {
              headers: {
                'Accept': 'application/json'
              }
            });

          case 2:
            jokeRes = _context.sent;
            _context.next = 5;
            return jokeRes.json();

          case 5:
            joke = _context.sent;
            jokeEl.textContent = joke.joke;

          case 7:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _generateJoke.apply(this, arguments);
}

I wrote this question when I had no idea how babel works. I now have some experience with it. I figured it out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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