在节点7.4中使用异步/等待 [英] Using async/await in node 7.4
问题描述
我认为节点7.4支持异步/等待,但是此示例不起作用:
I thought async/await was supported in node 7.4, however this example does not work:
const Promise = require('bluebird');
async function main(){
await Promise.delay(1000)
}
main();
结果:
async function main(){
^^^^^^^^
SyntaxError: Unexpected token function
如何对节点7.4使用异步/等待?
How can I use async/await with node 7.4?
推荐答案
是的,Node.js v7支持async-await,但将其锁定在harmony
标志后面.尚未准备就绪的功能不在此标记后面.
Yes async-await is supported in Node.js v7 but its locked behind the harmony
flag. Features which are not yet production ready are behind this flag.
要在Node.js v7中使用async-await,只需使用此标志运行Node服务-
To use async-await in Node.js v7 simply run Node service with this flag -
node --harmony-async-await app.js
async-await的正式发行版预定于4月启动的Node.js v8.
The official release of async-await is slated for Node.js v8 which will be launched in April.
您可以按照此拉取请求来检查其状态.基本上,async-await的正确功能取决于将V8引擎v5.5集成到Node.js中.当前,Node.js使用V8 v5.4,此拉取请求已解决该问题.
You can follow this pull request to check its status. Basically the correct functioning of async-await is dependent on the integration of V8 engine v5.5 into Node.js. Currently Node.js uses V8 v5.4 which is solved by this pull request.
更新1 -似乎V8 v5.5即将加入Node.js v7.请遵循此拉取请求以获取更多详细信息.
Update 1 - It seems V8 v5.5 might be coming to Node.js v7. Follow this pull request for more details.
更新2 -大家好!现在,Node.js版本7.6.0正式支持async
功能,而无需使用--harmony
标志,因为已成功移植V8引擎5.5.
Update 2 - Good news guys! Node.js version 7.6.0 now officially supports async
functions without using the --harmony
flag as V8 engine 5.5 has been successfully ported.
现在,如果您的Node.js版本在7.0到7.5.0(含)之间,则仅需要使用--harmony
标志.有关完整的更改日志,请此处.
Now you only need to use the --harmony
flag if your Node.js version is between 7.0 to 7.5.0 (inclusive). For complete changelog refer here.
这篇关于在节点7.4中使用异步/等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!