【问题标题】:Firebase Async Function Syntax ErrorFirebase 异步函数语法错误
【发布时间】:2017-10-16 18:28:58
【问题描述】:

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

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

还有我的等待/异步测试代码:

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)

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 仅供参考:不知道是谁投了反对票,但这是正确的答案。我是一名 Firebaser,与 Cloud Functions 团队密切合作。
    • 感谢您的信息,您知道何时支持 v7.6 吗?
    • 我们没有公布未来工作的具体时间表,但会在 10 月 31 日之后的某个时间(即 Node.js v8 进入 LTS)。
    • 不——只是 Node v8 将在 Cloud Functions 达到普遍可用性后的某个时间发生。我们建议同时使用e.g. TypeScript 来支持异步/等待。
    猜你喜欢
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    • 2018-02-10
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 2018-12-12
    相关资源
    最近更新 更多