Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using async/await in Node 6 with Babel

I'm trying to configure Babel for Node v6.9.2. I want to use async/await constructs.

Because I'm new to Babel and all Node infrastructure, I confused how to configure it properly:

  • What preset should I use? Node is already implemented most of the ES6 features. So I don't want Babel to transpile features already supported by Node 6.9.x (arrow functions, new import mechanism etc) for performance reasons.

  • What plugins should I include so I can use async/await? There I also confused, because after some researching I found several plugins: syntax-async-functions, transform-async-to-generator and some more.

Example of .babelrc will help.

Thanks

like image 315
WelcomeTo Avatar asked Feb 17 '17 13:02

WelcomeTo


People also ask

Does Babel support async await?

Babel needs a little extra love if you want to transpile Async/Await or Generator Functions (link to docs). Now you can Async, Await, and Generate to your hearts' content.

Can I use async await in node?

Async functions are available natively in Node and are denoted by the async keyword in their declaration. They always return a promise, even if you don't explicitly write them to do so. Also, the await keyword is only available inside async functions at the moment – it cannot be used in the global scope.

Is async await available in ES6?

Async and Await both are considered as special keywords which are provided by ES6 in order to perform some asynchronous data operations.

Which node version has async await?

With Node v8, the async/await feature was officially rolled out by the Node to deal with Promises and function chaining. The functions need not to be chained one after another, simply await the function that returns the Promise. But the function async needs to be declared before awaiting a function returning a Promise.


2 Answers

What preset should I use?

You don't need to use any preset. Presets are just a collection of plugins which makes it easier to use if you want to transpile a set of features (for instance all ES2015 with preset-es2015). But when you want to transpile only a selection of these features, you only include the corresponding plugins.

What plugins should I include so I can use async/await?

Because Node 6 supports generators, you can use transform-async-to-generator with the following .babelrc:

{
  "plugins": ["transform-async-to-generator"]
}

And of course you would need to add plugins if you need to transpile more unsupported features.

Alternative babel-preset-env

babel-preset-env automatically determines what plugins you need for the specified environment. This will not include any plugins that are not necessary. To specify your current Node version you would use this .babelrc:

{
  "presets": [
    ["env", {
      "targets": {
        "node": "current"
      }
    }]
  ]
}
like image 100
Michael Jungo Avatar answered Sep 22 '22 12:09

Michael Jungo


Short answer

Use Babel preset for Node 6.x:

  • https://www.npmjs.com/package/babel-preset-node6

Long answer

To see what ES feature is supported in a given Node version, see:

  • http://node.green/

For async/await support in particular, see:

  • http://node.green/#ES2017-features-async-functions

If you use Node v7.x (the current version) then you can use the --harmony flag and use async/await natively without transpilation.

Node v8.x (available as nightly builds) doesn't even need the --harmony flag for that.

But note that Node doesn't support import/export - to know why see:

  • javascript - Why is there a spec for sync and async modules?
  • Exporting Node module from promise result
like image 43
rsp Avatar answered Sep 19 '22 12:09

rsp