Skip to content

Instantly share code, notes, and snippets.

@thejmazz
Created February 16, 2016 18:17
Show Gist options
  • Star 71 You must be signed in to star a gist
  • Fork 16 You must be signed in to fork a gist
  • Save thejmazz/067295d9fb8b22c77be0 to your computer and use it in GitHub Desktop.
Save thejmazz/067295d9fb8b22c77be0 to your computer and use it in GitHub Desktop.
async/await with webpack+babel
{
"presets": ["es2015"],
"plugins": ["transform-async-to-generator"]
}
async function foo() {
console.log('async working!')
}
async function bar() {
await foo()
console.log('after foo')
}
bar()
{
"name": "async-await",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "MIT",
"devDependencies": {
"babel-loader": "^6.2.2",
"babel-plugin-transform-async-to-generator": "^6.5.0",
"babel-polyfill": "^6.5.0",
"babel-preset-es2015": "^6.5.0",
"webpack": "^1.12.13"
}
}
module.exports = {
entry: ['babel-polyfill', './index.js'],
output: {
filename: 'bundle.js'
},
devtool: 'sourcemap',
module: {
loaders: [{
test: /\.js?$/,
exclude: /(node_modules)/,
loader: 'babel'
}]
}
}
@Boluo2101
Copy link

Thanks

@mqliutie
Copy link

Is polyfill necessary ?

@qodesmith
Copy link

Wondering if polyfill is necessary also.

@cotyembry
Copy link

Its necessary if your wanting to use certain features - Im having to add it to include the async await features

@sadeghbarati
Copy link

how about babel 7 ???

every link I checked using the previous version of babel for doing this such thing

@desaijay315
Copy link

We need to use @babel/preset-env which transforms syntax. We need to use @babel/transform-runtime to avoid duplicate code, and config core-js: 3 to polyfill.

babel-plugin-transform-runtime automatically injects Regenerator where generators or async/await are used.

So, installing babel/runtime and @babel/plugin-transform-runtime need to be installed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment