'等待意想不到的标识符'对Node.js 7.5

Glynn Bird提出了一个问题:'await Unexpected identifier' on Node.js 7.5,或许与您遇到的问题类似。

回答者Glynn Bird给出了该问题的处理方式:

Thanks to the other commenters and some other research await can only be used in an async function e.g.

async function x() {
  var obj = await new Promise(function(resolve, reject) {
    setTimeout(function() {
      resolve({a:42});
    },100);
  });
  return obj;
}

I could then use this function as a Promise e.g.

x().then(console.log)

or in another async function.

Confusingly, the Node.js repl doesn't allow you to do

await x();

where as the RunKit notebook environment does.

希望本文对你有帮助,欢迎支持JavaScript中文网