问题描述
我无法获取异步/等待转换工作.我想念什么?
Im having trouble getting async / await transforms working.What am I missing?
我的.babelrc:
My .babelrc:
{
"presets": [ "es2015", "stage-0" ]
}
我的package.json(已删除):
My package.json (snipped):
{
"babel-core": "^6.1.2",
"babel-plugin-transform-runtime": "^6.1.2",
"babel-preset-es2015": "^6.1.2",
"babel-preset-stage-0": "^6.1.2"
}
输出:
babel src/server
SyntaxError: src/server/index.js: Unexpected token (7:21)
5 |
6 | try {
> 7 | let server = await server('localhost', env.NODE_PORT || 3000)
| ^
8 | console.log(`Server started on ${server.info.uri}`)
9 | } catch (err) {
10 | console.error('Error starting server: ', err)
推荐答案
根据此帖子,您需要拥有babel-polyfill
According to this post you need to have babel-polyfill
Babel 6 regeneratorRuntime没有用async/await定义
希望它会对您有所帮助:)
Hopefully it'll help you :)
它不一定是babel-polyfill,但它是我唯一使用过的.
It doesn't have to be babel-polyfill but it's the only one I used.
正如Gothdo所说:await
关键字必须在函数范围内.此外,此函数定义必须具有async
关键字.
As Gothdo said: the await
keyword has to be in a function scope. Moreover, this function definition has to have the async
keyword.
这意味着您不能在顶级范围内使用await
关键字.
This means that you can not have the await
keyword on the top-level scope.
这篇关于babel 6异步/等待:意外令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!