検索

ホームページ  >  に質問  >  本文

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

怎么破?

天蓬老师天蓬老师2868日前672

全員に返信(4)返信します

  • PHPz

    PHPz2017-04-17 15:10:33

    ノードは Chrome 55 Stable のリリース後に正式に async/await をサポートしますが、最新の V7 バージョンは --harmony モードがオンになっている場合にサポートできます。

    返事
    0
  • PHPz

    PHPz2017-04-17 15:10:33

    babel でコンパイルします

    返事
    0
  • 迷茫

    迷茫2017-04-17 15:10:33

    これを追加して、node --harmony app.js を試してください

    返事
    0
  • PHP中文网

    PHP中文网2017-04-17 15:10:33

    nodeJS v7 でのみサポートされています

    返事
    0
  • キャンセル返事