Using ES2016 (ES7) async/await with Babel

UPDATE: as of October 2016, Node 7 is out, and it supports async/await natively.

Original post follows.

async/await make JavaScript code far easier to think about, solving the problem of callback hell for good (for which there are thousands of libraries — but none as clear and simple as the async/await paradigm).

Since async/await aren’t yet available in Node.js or any browsers as of June 2016, you have to use a transpiler. Here’s the simplest way to enable aync/await using Babel and Node.js 4.x or later:

1. Install Babel and the async plugin:

npm install babel-cli babel-plugin-transform-async-to-generator --save-dev

2. In your package.json file (instead of .babelrc), add:

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

Now you can run your code with babel-node in your project directory:

node_modules/babel-cli/bin/babel-node.js myscript

That’s it. you don’t need the es2015 preset or anything else.

Bonus — WebStorm trick

To use async/await in WebStorm, you don’t need file watchers or source maps. Simply configure your “node” executable to point to babel-node: