Firebase 异步等待风格的云函数 [英] Cloud Functions for Firebase Async Await style

查看:27
本文介绍了Firebase 异步等待风格的云函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来 Cloud Functions 不支持 Async-Await 表示法.有没有办法在他们使用 Babel 之前使用它们,或者是否建议使用 Promise?

Looks like Cloud Functions does not support Async-Await notation. Is there a way I could use Babel until they do or is it recommended to use promises?

我当前在 Node 上的函数是这样的:

My current function that sits on Node is like so:

exports.getToken = async (req, res) => {
  //1. Generate token from Braintree
  const result = await gateway.clientToken.generate();

  //2. Return the client token
  res.json(result.clientToken);
};

推荐答案

Cloud Functions 运行 LTS 版本的 node.js,根据 documentation 目前是 6.14.0.node 6.x 支持 EcmaScript 6,不包括 async/await.

Cloud Functions runs the LTS version of node.js, which according to the documentation is 6.14.0 at this moment in time. node 6.x supports EcmaScript 6, which does not include async/await.

但是,您可以使用 TypeScript 编写代码并将其转换为 ES5/ES6,这将有效地将 async/await 的使用转换为 Promise.网络搜索表明,也许 这个插件 可以用来帮助 Babel 进行类似的转译.

However, you can write your code in TypeScript and have that transpiled down to ES5/ES6, which will effectively convert the use of async/await into promises. A web search suggests that perhaps this plugin can be used to help Babel with similar transpiling.

值得注意的是,Firebase CLI 现在允许您使用 本机 TypeScript 初始化新的 Cloud Functions 项目支持,这是 Firebase 团队目前向开发者推荐的.

It's worth noting that the Firebase CLI now allow you to initialize a new Cloud Functions project with native TypeScript support, which is what the Firebase team is currently recommending to developers.

如果您不想使用 TypeScript,您现在还可以选择节点 8(目前处于测试阶段,并且支持纯 JavaScript 的 async/await)作为部署目标.您可以按照文档编辑您的 package.json 以表明您的功能应部署到节点 8.

If you don't want to use TypeScript, you can now also choose node 8 (which is currently in beta, and does support async/await for plain JavaScript) as a deployment target. You can follow the documentation to edit your package.json to indicates that your functions should be deployed to node 8.

这篇关于Firebase 异步等待风格的云函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆