Firebase Async Await样式的云功能 [英] Cloud Functions for Firebase Async Await style

查看:58
本文介绍了Firebase Async Await样式的云功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来像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,根据不包括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(当前处于beta中,并且确实支持纯JavaScript的异步/等待)作为部署目标.您可以按照文档来编辑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 Async Await样式的云功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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