Javascript babel 6 异步/等待:意外的令牌
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33641593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
babel 6 async / await: Unexpected token
提问by legomind
Im having trouble getting async / await transforms working. What am I missing?
我无法让异步/等待转换工作。我错过了什么?
My .babelrc:
我的 .babelrc:
{
"presets": [ "es2015", "stage-0" ]
}
My package.json (snipped):
我的 package.json (剪下):
{
"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"
}
Output:
输出:
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)
回答by Yormi
According to this post you need to have babel-polyfill
根据这篇文章,你需要有 babel-polyfill
Babel 6 regeneratorRuntime is not defined with async/await
Babel 6 regeneratorRuntime 没有用 async/await 定义
Hopefully it'll help you :)
希望它会帮助你:)
EDIT:
编辑:
It doesn't have to be babel-polyfill but it's the only one I used.
它不一定是 babel-polyfill,但它是我唯一使用的。
As Gothdo said: the await
keyword has to be in a function scope. Moreover, this function definition has to have the async
keyword.
正如 Gothdo 所说:await
关键字必须在函数范围内。而且,这个函数定义必须有async
关键字。
This means that you can not have the await
keyword on the top-level scope.
这意味着您不能await
在顶级范围内使用关键字。
回答by Allain Lalonde
Looks like async/await is only available in babel-preset-stage-3
看起来 async/await 仅适用于 babel-preset-stage-3
回答by Madole
You can compile them yourself using the transform-async-to-module-method
plugin, this allows you to compile them down to bluebird co-routines which requires ES6 generators (available in node4).
您可以使用transform-async-to-module-method
插件自己编译它们,这允许您将它们编译为需要 ES6 生成器(在 node4 中可用)的 bluebird 协程。
Or if you need to compile it back to ES5 so it's compatible for browsers you can use transform-async-to-generator
and facebook's regenerator
.
或者,如果您需要将其编译回 ES5,以便与您可以使用的浏览器transform-async-to-generator
和 facebook 的regenerator
.
I've written about how to set up your babel config here http://madole.xyz/async-await-es7/
我已经在http://madole.xyz/async-await-es7/写了关于如何设置你的 babel 配置的文章
回答by Beder Acosta Borges
Use the Async to generator transform.
使用Async 到生成器转换。
Installation
安装
$ npm install babel-plugin-transform-async-to-generator
Usage
用法
Add the following line to your .babelrc
file:
将以下行添加到您的.babelrc
文件中:
{
"plugins": ["transform-async-to-generator"]
}
回答by Tamizaan
It's recommended to upgrade to Babel 7 and use babel-env as opposed to stages (see here: https://github.com/babel/babel-upgrade).
建议升级到 Babel 7 并使用 babel-env 而不是阶段(请参阅此处:https: //github.com/babel/babel-upgrade)。
There's a command you can use to upgrade accordingly:
有一个命令可以用来相应地升级:
npx babel-upgrade