Firebase异步函数语法错误

时间:2017-10-16 18:28:58

标签: node.js firebase asynchronous google-cloud-functions

我有node.js v8.3.0,并在我的package.json中有这个:

  "engines": {
    "node": ">=8.3.0"
  }

我的测试代码是await / async:

async function x() {
    return "test";
}

exports.asyncTest = functions.https.onRequest((request, response) => async function() {
    response.end(await x());
});

预期输出:test
观察到的输出:

Is there a syntax error in your code?
Detailed stack trace: /user_code/index.js:12
async function x() {
      ^^^^^^^^
SyntaxError: Unexpected token function
    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 Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at getUserFunction (/var/tmp/worker/worker.js:372:24)

1 个答案:

答案 0 :(得分:10)

Firebase运行时的Cloud Functions目前是Node.js v6.x,因此不支持async / await。期望Node v8.x在进入长期支持(LTS)后的某个时间得到支持。

与此同时,您需要使用像Babel或TypeScript这样的转换器来利用async / await。