Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

node.js - Using async/await in node 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();

Results in:

async function main(){
      ^^^^^^^^
SyntaxError: Unexpected token function

How can I use async/await with node 7.4?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

To use async-await in Node.js v7 simply run Node service with this flag -

node --harmony-async-await app.js

The official release of async-await is slated for Node.js v8 which will be launched in April.

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.

Update 1 - It seems V8 v5.5 might be coming to Node.js v7. Follow this pull request for more details.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...