使用异步等待与babel时出现意外的令牌

时间:2016-03-05 16:28:41

标签: javascript node.js async-await babeljs

使用带babel 6的async / await时获取意外的令牌

boot.js

require("babel-core/register")({
  ignore: "./node_modules"
});
require("babel-polyfill");

asyncAwaitSample.js

async function() {
    await someAsyncAction();
  }()

.babelrc

{
  "presets": ["es2015", "stage-0", "react"]
}

编译器在字符串async function() {上出现错误,它说“意外令牌”{'“

1 个答案:

答案 0 :(得分:2)

你安装了babel-plugin-transform-async-to-generator吗?

npm install babel-plugin-transform-async-to-generator

并在.babelrc

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

来源https://babeljs.io/docs/plugins/transform-async-to-generator/

然后执行:

(async function() {
  await someAsyncAction();
})()