node.js - node v6.x 不支持 async await 吗?

查看:173
本文介绍了node.js - node v6.x 不支持 async await 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

node版本是v6.2.2

新建一个app.js内容如下

async function responseTime(ctx, next) {
  const start = new Date();
  await next();
  const ms = new Date() - start;
  ctx.set('X-Response-Time', `${ms}ms`);
}

然后node app.js报如下错误:

mbp:hybrid-statistic wfp$ node app.js
/Users/wfp/Work/hybrid-statistic/app.js:28
async function responseTime(ctx, next) {
      ^^^^^^^^
SyntaxError: Unexpected token function
    at Object.exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:513:28)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Module.runMain (module.js:575:10)
    at run (node.js:348:7)
    at startup (node.js:140:9)
    at node.js:463:3

怎么破?

解决方案

Node会在Chrome 55 Stable发布后正式支持async/await,不过最新的V7版本在打开--harmony的模式的时候可以支持.

这篇关于node.js - node v6.x 不支持 async await 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆