Firebase异步函数语法错误

Tal*_*göz 4 asynchronous node.js firebase google-cloud-functions

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

  "engines": {
    "node": ">=8.3.0"
  }
Run Code Online (Sandbox Code Playgroud)

我的await/async测试代码:

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

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

预期产出: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)
Run Code Online (Sandbox Code Playgroud)

Mic*_*igh 10

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

在此期间,您需要使用像Babel或TypeScript这样的转换器来利用async/await.

  • 仅供参考:不确定是谁贬低了这一点,但这是正确的答案.我是Firebaser,并与Cloud Functions团队密切合作. (2认同)
  • 我们没有公布未来工作的具体时间表,但将在10月31日之后的某个时间(这是Node.js v8进入LTS时). (2认同)