【发布时间】:2017-05-21 14:25:43
【问题描述】:
我正在尝试在 NodeJS 中使用 async/await,但我的脚本抛出了语法错误。
我的印象是 async/await 是 supported naively since Node 7.6。当我运行node -v 时,我得到v7.10.0。
这里是index.js的内容:
async function getValueAsync() {
return new Promise(function(resolve) {
resolve('foo');
});
}
let value = await getValueAsync();
console.log(value);
但是当我用 node index.js 调用这个脚本时,我得到:
let value = await getValueAsync();
^^^^^^^^^^^^^
SyntaxError: Unexpected identifier
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
我正在运行 Linux Mint 18.1。
如何让我的脚本编译和运行?
【问题讨论】:
标签: node.js async-await syntax-error