0

I have configured Babel for my project with ".babelrc" file. My .babelrc file is,

{
"presets": [
  [
    "@babel/preset-env",
    {
      "useBuiltIns": "entry"
    }
  ]
]

}

I have imported "core-js/stable" and "regenerator-runtime/runtime" using index.js in my index.html as follows. I'm using Parcel as the packaging tool.

<!DOCTYPE html>
<html lang="en">

<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <meta http-equiv="X-UA-Compatible" content="ie=edge">
   <script src="../js/index.js"></script>
   <title>Document</title>
</head>

<body>
  <h1>Hello Second App</h1>
  <script>

    (() => {
        console.log('welcome ...');
    })();

    function getUserById(id) {
        return Promise.try(function () {
            if (typeof id !== "number") {
                throw new Error("id must be a number");
            }
            return "done";
        });
    }

    getUserById();

  </script>
</body>

</html>

Also as you can see I'm trying to use "Promise.try". But I'm getting

Promise.try is not a function

So why Babel is not fixing this? Please help me. I'm trying to understand Babel.

  • That's *not* a method of native promises: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#Static_methods – jonrsharpe May 02 '20 at 11:16
  • I'm no longer up to date with ECMAScript but it seems to be a [draft](https://tc39.es/proposal-promise-try/) so you possibly need [@babel/preset-stage-1](https://babeljs.io/docs/en/babel-preset-stage-1), but docs say it's deprecated. – Álvaro González May 02 '20 at 11:20
  • So can you please explain the difference between preset-env and prest-stage-1 ? – Thathsara Udugampala May 02 '20 at 11:47
  • since I'm using preset-env I guess it targets all environments right? – Thathsara Udugampala May 02 '20 at 11:48
  • The difference is that `env` [doesn't do what you want](https://babeljs.io/docs/en/babel-preset-env#how-does-it-work): "It is important to note that @babel/preset-env does not support stage-x plugins". – Álvaro González May 02 '20 at 11:54

0 Answers0