无法在 Cloud9 上的节点上创建异步方法

Cannot create async methods on Node on Cloud9

我正在移植我在浏览器中编写的一些代码,发现我似乎无法在 NodeJS 中创建异步方法

class Test{
    async hello(){
        return "hello";
    }
}

(async function(){
    let test = new Test();
    let hello = await test.hello();
    console.log(hello);
})();

当我执行这个时,我得到一个错误:

/home/ubuntu/workspace/test.js:2
    async hello(){
          ^^^^^

SyntaxError: Unexpected identifier
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Timeout.Module.runMain [as _onTimeout] (module.js:604:10)
    at ontimeout (timers.js:386:14)
    at tryOnTimeout (timers.js:250:5)

这在 node 中是不可能的,还是我这里没有什么不正确的地方?

我是运行Node 8.x

该代码中发生的错误如下:

let hello = await test.hello();
                  ^^^^

SyntaxError: Unexpected identifier

这是因为您在 async 函数之外使用了 await 关键字。

来自docs

The await operator is used to wait for a Promise. It can only be used inside an async function.

class Test{
    async hello(){
        return "hello";
    }
}


(async() => {
  // You can only use `await` inside async function
  let test = new Test();
  let hello = await test.hello();
  console.log(hello);
})();


Is this just not possible in node, or am I don't something incorrect here? 
I am running Node 8.x

Node从7.6版本开始支持async/await,可以自由使用

更新:

如果您得到:

async hello(){
      ^^^^^

SyntaxError: Unexpected identifier

这意味着您 运行 使用的是较旧的节点版本。尝试

console.log(process.version);

并且将 100% 打印低于 7.6 的版本。

您的 cli 上可能有节点 8.x,但 cloud9 上没有,要更新 cloud9 中的节点,请检查以下问题: