NodeJS script with async/await causing syntax error (v7.10.0)

女生的网名这么多〃 提交于 2019-11-28 11:57:24

await is only valid inside async functions, so you need, for example, an async IIFE to wrap your code with:

void async function() {
  let value = await getValueAsync();
  console.log(value);
}();

And, since return values from async functions are wrapped by a promise, you can shorten getValueAsync to simply this:

async function getValueAsync() {
  return 'foo';
}

Or don't mark it as async and return a promise from it:

function getValueAsync() {
  return new Promise(function(resolve) {
    resolve('foo');
  });
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!