问题描述
我正在尝试使用async,等待Babel 6从头开始,但我没有定义regeneratorRuntime。
I'm trying to use async, await from scratch on Babel 6, but I'm getting regeneratorRuntime is not defined.
.babelrc文件
.babelrc file
{
"presets": [ "es2015", "stage-0" ]
}
package.json file
package.json file
"devDependencies": {
"babel-core": "^6.0.20",
"babel-preset-es2015": "^6.0.15",
"babel-preset-stage-0": "^6.0.15"
}
.js文件
"use strict";
async function foo() {
await bar();
}
function bar() { }
exports.default = foo;
在没有async / await的情况下正常使用它可以正常工作。我有什么想法吗?
Using it normally without the async/await works just fine. Any ideas what I'm doing wrong?
推荐答案
babel-polyfill
是必须的。您还必须安装它才能使异步/等待工作。
babel-polyfill
is required. You must also install it in order to get async/await working.
npm i -D babel-core babel-polyfill babel-preset-es2015 babel-preset-stage-0 babel-loader
package.json
package.json
"devDependencies": {
"babel-core": "^6.0.20",
"babel-polyfill": "^6.0.16",
"babel-preset-es2015": "^6.0.15",
"babel-preset-stage-0": "^6.0.15"
}
.babelrc
{
"presets": [ "es2015", "stage-0" ]
}
.js with async / await(示例代码)
.js with async/await (sample code)
"use strict";
export default async function foo() {
var s = await bar();
console.log(s);
}
function bar() {
return "bar";
}
在启动文件中
require("babel-core/register");
require("babel-polyfill");
如果您使用 webpack ,则需要将其作为第一个条目根据@Cemen评论:
If you are using webpack you need to put it as the first entry as per @Cemen comment:
module.exports = {
entry: ['babel-polyfill', './test.js'],
output: {
filename: 'bundle.js'
},
module: {
loaders: [
{ test: /\.jsx?$/, loader: 'babel', }
]
}
};
如果您想使用babel运行测试,请使用:
If you want to run tests with babel then use:
mocha --compilers js:babel-core/register --require babel-polyfill
这篇关于未定义Babel 6 regeneratorRuntime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!